python 파충류로 pdf 대량 다운로드 실현

오늘 excel 파일에 500여 개의 pdf 파일의 다운로드 링크가 있는 작업을 만났습니다. 이 파일들을 모두 다운로드해야 합니다.나는python 파충류로 대량으로 다운로드할 수 있다는 것을 알고 있지만, 이전에 접촉한 적이 없다.오늘 오후에 자료를 찾아서 마침내 성공적으로 끝내서 수동 다운로드의 번뇌를 면했다.
내가 구축한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를 대량으로 다운로드하는 내용은 저희 이전의 글을 검색하거나 아래의 관련 글을 계속 훑어보시기 바랍니다. 앞으로 많은 응원 부탁드립니다!

좋은 웹페이지 즐겨찾기