python爬取图

import requests
from lxml import etree
if __name__ == "__main__":
    url='https://pic.netbian.com/4kdongman/index_%d.html'
    headers={
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36"
    }
    a=eval(input('请输入起始页'))
    b=eval(input('请输入终止页'))
    for num in range(a,b):
        newurl=format(url%num)
        response=requests.get(url=newurl,headers=headers)
        response.encoding='gbk'
        text=response.text
        tree=etree.HTML(text)
        list=tree.xpath('//div[@class="slist"]/ul/li')
        for li in list:
            c_url='https://pic.netbian.com'+li.xpath('./a//@href')[0]
            c_response=requests.get(url=c_url,headers=headers)
            c_text=c_response.text
            c_tree=etree.HTML(c_text)
            c_list=c_tree.xpath('//div[@class="photo-pic"]/a/img/@src')[0]
            lasturl='https://pic.netbian.com'+c_list
            l_response=requests.get(url=lasturl,headers=headers)
            l_response.encoding='gbk'
            name = c_tree.xpath('//div[@class="photo-pic"]/a/img/@alt')[0]
            name=name.encode('iso-8859-1').decode('gbk')
            date=l_response.content
            path = 'D:\\图片\\' + name +'.jpg'# 根据自己需要改这里(name之前)
            with open(path, 'wb') as p:
                p.write(date)
            print(name, '爬取成功')
print('爬取完成')

猜你喜欢

转载自blog.csdn.net/qq_52351946/article/details/131616137
今日推荐