Python3-requests-xpath-抓取圖片並保存

Python3-requests-xpath-抓取圖片並保存

一(來自別人的)

import requests
from lxml import etree
j=0
#設置的翻頁
for i in range(0,2):
    r=requests.get('https://book.douban.com/tag/%E7%A5%9E%E7%BB%8F%E7%BD%91%E7%BB%9C?'+'start=%d&type=T'%i*20).content
    books=etree.HTML(r)
    抓取圖片的鏈接 通過xpath
    imgs=books.xpath('//*[@id="subject_list"]/ul/li/div[1]/a/img/@src', stream=True)
    #保存位置
    path = "E:/IMG/"
    for img in imgs:
        j=j+1
        #------------位置+保存的名稱(名稱此處是遞增的 j)
        with open(path+str(j)+'.jpg', 'wb') as fd:
            picture=requests.get(img).content
            fd.write(picture)

原文鏈接–https://blog.csdn.net/weixin_39777626/article/details/79300856

方法二

import urllib
urllib.urlretrieve('圖片鏈接http://', 'f:/自己的本地地址.jpg')
發佈了13 篇原創文章 · 獲贊 14 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章