python爬虫学习05-爬取图片

python爬虫学习05-爬取图片

  1. 确定要爬取的网址:https://shenan.tuchong.com/20903415/#image309854686

  2. 要爬取的内容:使用浏览器插件xpath对图片链接进行查找://article/img/@src

  3. 得到图片链接:

  1. 代码
import requests
from fake_useragent import UserAgent
from lxml import etree

url = "https://shenan.tuchong.com/20903415/#image309854686"
headers = {
    "UserAgent":UserAgent().chrome
}
response = requests.get(url,headers=headers)
e = etree.HTML(response.text)
img_urls = e.xpath('//article/img/@src') #获取图片链接
print(img_urls)
for url in img_urls:
    response = requests.get(url,headers=headers)
    img_name = url[url.rfind('/')+1:]   #命名
    with open('img/'+img_name,'wb') as f:   #写入到已存在的img文件夹中
        f.write(response.content)
posted @ 2020-08-03 11:46  侠客小飞  阅读(207)  评论(0编辑  收藏  举报