+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2019-07(2)

2019-08(106)

2019-09(110)

2019-10(14)

2019-11(8)

Python爬虫练习:汽车之家图片爬虫

发布于2020-05-23 17:04     阅读(1195)     评论(0)     点赞(3)     收藏(0)


0

1

2

3

4

import requests
from lxml import etree
import re,time,os
class carDownload:
    def __init__(self):
        self.header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'}
    def getCarUrl(self,url):
        content=requests.get(url,headers=self.header).text
        dom=etree.HTML(content)
        urls=dom.xpath('//@href')
        page_urls=[url]
        for i in urls:
            if '/pic/series/614-1-p' in i:
                page_urls.append('https://car.autohome.com.cn/pic'+i)
        return list(set(page_urls))
    def getCarPicUrl(self,url):
        content=requests.get(url,headers=self.header).text
        dom=etree.HTML(content)
        urls=dom.xpath('//@href')
        page_urls=[]
        for i in urls:
            if '/photo/series/' in i:
                page_urls.append('https://car.autohome.com.cn/'+i)
        return list(set(page_urls))
    def getPic(self,name,url):
        content=requests.get(url,headers=self.header).text
        dom=etree.HTML(content)
        urls=dom.xpath('//*[@id="main"]/div[1]/img/@src')[0]
        with open(f'./{name}/'+str(time.time())+'.jpg','wb') as f:
            f.write(requests.get('https:'+urls).content)
            print("保存成功")
    def main(self,path,url):
        if not os.path.exists(path):
            os.mkdir(path)
        carPage=self.getCarUrl(url)
        for i in carPage:
            for n in self.getCarPicUrl(i):
                self.getPic(path,n)
car=carDownload()
car.main('君威','https://car.autohome.com.cn/pic/series/164-1-p1.html')

main(文件夹名称,汽车图片路径)
在这里插入图片描述

原文链接:https://blog.csdn.net/qq_44198436/article/details/106271159

0

1

2

3

4

5



所属网站分类: 技术文章 > 博客

作者:9384vfnv

链接: https://www.pythonheidong.com/blog/article/390411/fcaad3771ffe0137cba7/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)