• 【Python爬虫实战】 图片爬虫-淘宝图片爬虫--千图网图片爬虫


    所谓图片爬虫,就是从互联网中自动把对方服务器上的图片爬下来的爬虫程序。
    有些图片是直接在html文件里面,有些是隐藏在JS文件中,在html文件中只需要我们分析源码就能得到
    如果是隐藏在JS文件中,那么就需要进行抓包分析,这儿先只讲分析html源码得出图片,
    注意 这儿我们需要读取的是高清原图,不是经过网站处理过的小图片。

    首先需要根据网址进行分析,分析出每一类商品的第几页第几页的网址之间的关联进行自动加载指定页码(例如淘宝每下一页为链接中s加44)

    然后查看页面源码,找到图片对应的链接,分析剔除掉后加修饰过的内容,将关键的内容截取在源码中进行搜索即可找到图片的原始地址
    也就是源码的地址。根据这个就能得到所要构造的正则表达式。
    然后直接上代码

    import urllib.request
    import re
    import urllib.error
    
    keyname = "短裙"
    key = urllib.request.quote(keyname) #进行编码
    
    #伪装浏览器 (因为淘宝能够识别是否为爬虫程序)
    headers = ("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36")
    opener = urllib.request.build_opener()
    opener.addheaders = [headers]
    #将opener添加为全局
    urllib.request.install_opener(opener)
    
    #要爬取多少页那么进行多少次循环
    for i in range(0,2):
        url = "https://s.taobao.com/search?q="+key+"&imgfile=&js=1&stats_click=search_radio_all%3A1&initiative_id=staobaoz_20171209&ie=utf8&bcoffset=4&ntoffset=4&p4ppushleft=1%2C48&s="+str(i * 44)
        #先对所在的页面的主页进行爬取读取内容,也就是读取源码
        data = urllib.request.urlopen(url).read().decode("utf-8","ignore")
        #构造正则表达式
        pattern = 'pic_url":"//(.*?)"'
        #在当前页根据正则进行查找,查找到的所有连接存储为一个list
        imagelist = re.compile(pattern).findall(data)
        #遍历列表进行每个图片的存储到本地文件夹
        for j in range(0,len(imagelist)):
            thisimg = imagelist[j]
            thisimageurl = "http://"+thisimg
            file = "E://pythoncode/taobaoimg1/"+"b"+str(i)+str(j)+".jpg"
            urllib.request.urlretrieve(thisimageurl,file)


    同理进行一个千图网(
    http://www.58pic.com/)的高清原图的爬取
    第一步也是分析网站的源码找到规律实现下一页的加载,然后进行图片链接的分析得到真实高清原图的网址链接构建正则表达式
    先对当前页进行爬取,从爬取的内容中使用正则进行页面查找,再对找到的每一个图片链接进行爬取存储到本地文件夹中。
    直接上代码

    import urllib.request
    import urllib.error
    import re
    
    key = "chengshi"
    
    proxy = urllib.request.ProxyHandler({"http":"202.96.142.2:3128"})
    
    opener = urllib.request.build_opener(proxy,urllib.request.HTTPHandler)
    urllib.request.install_opener(opener)
    
    for i in range(1,2):
        url = "http://www.58pic.com/tupian/"+key+"-0-0-"+str(i)+".html"
        data = urllib.request.urlopen(url).read().decode("utf-8","ignore")
        pattern = '"(http://pic.qiantucdn.com/58pic/.*?)!'
        imagelist = re.compile(pattern).findall(data)
        # print(imagelist)
        for j in range(0,len(imagelist)):
            thisurl = imagelist[j]
            file = "E:/pythoncode/qiantu/"+str(i)+str(j)+".jpg"
            urllib.request.urlretrieve(thisurl,file)
    
    
    







  • 相关阅读:
    [bigdata] storm集群安装及测试
    [bigdata] kafka集群安装及测试
    [bigdata] spark集群安装及测试
    [bigdata] 使用Flume hdfs sink, hdfs文件未关闭的问题
    [bigdata] 启动CM出现 “JDBC Driver class not found: com.mysql.jdbc.Driver” 以及“Error creating bean with name 'serverLogFetcherImpl'”问题的解决方法
    [JavaEE]设计模式之SOLID原则
    [Android]Volley源码分析(五)
    [Android]Volley源码分析(四)
    [Android]Volley源码分析(三)
    python学习Day13--生成函数
  • 原文地址:https://www.cnblogs.com/Liuyt-61/p/8040168.html
Copyright © 2020-2023  润新知