由于工作需要,不得不学习下爬虫。小甲鱼书上第188页的题目。(书上给的站点已经不能访问了,改成下面这个)
import urllib.request
import re
import os
def open_url(url):
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36') #伪造成浏览器请求
page = urllib.request.urlopen(req)
html = page.read().decode('utf-8')
return html
def get_img(html):
p = r'<img class="BDE_Image".*?src="([^"]*\.jpg)".*?>'
imglist = re.findall(p, html)
try:
os.mkdir("NewPics")
except FileExistsError:
pass
os.chdir("NewPics")
for each in imglist:
print(each) # each = http://imgsrc.baidu.com/forum/w%3D580/sign=b45f5974f803738dde4a0c2a831ab073/cefe9b510fb30f244ff9cad7c695d143ac4b037d.jpg
filename = each.split("/")[-1] # 从each中截取出图片的名字
print(filename) # filename = cefe9b510fb30f244ff9cad7c695d143ac4b037d.jpg
urllib.request.urlretrieve(each, filename, None) # 利用urllib.request.urlretrieve() 将图片保存到本地
if __name__ == '__main__':
url = "http://c.tieba.baidu.com/p/6212373364"
get_img(open_url(url))
大家一起加油!