python 파충류로 pdf 대량 다운로드 실현
내가 구축한python 버전은 3.5이기 때문에 나는 위에서 열거한 참고 문헌 2의 코드를 배웠다. 이곳의 버전은 2.7이고 일부 문법은 이미 적용되지 않는다.나는 다음과 같은 일부 문법을 수정했다.
# coding = UTF-8
# PDF , :http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm
import urllib.request
import re
import os
# open the url and read
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
page.close()
return html
# compile the regular expressions and find
# all stuff we need
def getUrl(html):
reg = r'(?:href|HREF)="?((?:http://)?.+?\.pdf)'
url_re = re.compile(reg)
url_lst = url_re.findall(html.decode('gb2312'))
return(url_lst)
def getFile(url):
file_name = url.split('/')[-1]
u = urllib.request.urlopen(url)
f = open(file_name, 'wb')
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
f.write(buffer)
f.close()
print ("Sucessful to download" + " " + file_name)
root_url = 'http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/'
raw_url = 'http://www.math.pku.edu.cn/teachers/lidf/docs/textrick/index.htm'
html = getHtml(raw_url)
url_lst = getUrl(html)
os.mkdir('ldf_download')
os.chdir(os.path.join(os.getcwd(), 'ldf_download'))
for url in url_lst[:]:
url = root_url + url
getFile(url)
위의 이 예는 매우 좋은 틀이다.물론 위의 것은 나의 상황에 적용되지 않는다. 나의 방법은 먼저 주소를 html 파일에 쓴 다음에 정규 일치 부분에 대해 수정을 했다. 내가 일치해야 할 주소는 모두 이렇다.http://pm.zjsti.gov.cn/tempublicfiles/G176200001/G176200001.pdf.개선된 코드는 다음과 같습니다.
# coding = UTF-8
# html PDF , :file:///E:/ZjuTH/Documents/pythonCode/pythontest.html
import urllib.request
import re
import os
# open the url and read
def getHtml(url):
page = urllib.request.urlopen(url)
html = page.read()
page.close()
return html
# compile the regular expressions and find
# all stuff we need
def getUrl(html):
reg = r'([A-Z]\d+)' # G176200001
url_re = re.compile(reg)
url_lst = url_re.findall(html.decode('UTF-8')) #
return(url_lst)
def getFile(url):
file_name = url.split('/')[-1]
u = urllib.request.urlopen(url)
f = open(file_name, 'wb')
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
f.write(buffer)
f.close()
print ("Sucessful to download" + " " + file_name)
root_url = 'http://pm.zjsti.gov.cn/tempublicfiles/' #
raw_url = 'file:///E:/ZjuTH/Documents/pythonCode/pythontest.html'
html = getHtml(raw_url)
url_lst = getUrl(html)
os.mkdir('pdf_download')
os.chdir(os.path.join(os.getcwd(), 'pdf_download'))
for url in url_lst[:]:
url = root_url + url+'/'+url+'.pdf' #
getFile(url)
쉽게 해결할 수 있을 거예요.나는 다음과 같은 자료를 참고했다. 이것은 나에게 매우 도움이 된다.
1、 랴오설봉python 강좌
2、 Python 파충류로 PDF 문서 대량 다운로드
3、 파이썬 파충류로 스티커 찾기
4、 파이썬 파충류 학습 시리즈 강좌
python 파충류로 pdf를 대량으로 다운로드하는 실현에 관한 이 글은 여기까지 소개되었습니다. 더 많은 python 파충류로 pdf를 대량으로 다운로드하는 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보시기 바랍니다. 앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
로마 숫자를 정수로 또는 그 반대로 변환그 중 하나는 로마 숫자를 정수로 변환하는 함수를 만드는 것이었고 두 번째는 그 반대를 수행하는 함수를 만드는 것이었습니다. 문자만 포함합니다'I', 'V', 'X', 'L', 'C', 'D', 'M' ; 문자열이 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.