最近在学沐神的动手学深度学习课程(非常感谢沐神,沐神YYDS)。但是d2l的材料美中不足的是每一个PPT都是每小节上传一个,手动下载会很麻烦,所以我写了一个爬虫(爬虫的水平tcl,能用就行,哈哈哈)。
下面是具体的代码,运行环境Python3,使用urllib
和BeautifulSoup
。
import urllib.request
import requests
from bs4 import BeautifulSoup
from time import sleep
url = 'https://courses.d2l.ai/zh-v2/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) '
'AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE',
# 'referer': url.encode("utf-8").decode("latin1")
}
response = requests.get(url, headers=headers)
result = response.content.decode('utf-8')
# response_1.encoding = 'gbk'
soup = BeautifulSoup(result, features='html.parser')
tag = []
for k in soup.find_all('a'):
if 'pdf' in k['href']:
tag.append(k['href'])
print(tag)
for pdf in tag:
urllib.request.urlretrieve(url+pdf, './pdf_download/{}'.format(pdf.split('/')[-1]))
print(pdf.split('/')[-1])
sleep(1)
最后祝大家学习顺利!