爬虫爬取图片
刚开始学习爬虫从网站上爬取一只猫:
import urllib.request
response = urllib.request.urlopen('http://placekitten.com/g/500/600')
img = response.read()
with open('cat -500-600','wb') as f:
f.write(img)
模仿上面这是用urllib.request模块实现的爬图,出错了:
import requests:
url =( 'http://placekitten.com/g/500/600')
img = requests.get(url)
with open('cat -500-600','wb') as f:
f.write(img)
出错了......错在哪里了,还有这两个模块都能实现爬取图片,他们区别在哪里,哪个更好用一点 你的两个文件打开cat -500-600没有加后缀.jpg
这两个模块第一个是python标准库,不太好用,第二个requests是python的第三方库,基于urllib库制作,对新手友好,学习难度低,好用 import requests后多了个: import urllib.request
import requests
url ='http://placekitten.com/g/500/600'
response = urllib.request.urlopen(url)
img = response.read()
with open('cat-500-600-urllib.jpg', 'wb') as f:
f.write(img)
img = requests.get(url).content
with open('cat-500-600-requests.jpg', 'wb') as f:
f.write(img)
看书
页:
[1]