Python爬虫案例之图片爬取

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

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

  • 得到图片链接:

    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-12-15 09:09  HackerEarl  阅读(260)  评论(0编辑  收藏  举报