scrapy爬取百度图片

百度图片基本没什么反爬虫措施,我们爬取图片时直接鼠标右键--->检查----->network---->XHR,往下拖动得到页面,可以看到headers下的General,查看实际的请求Request URL,提取其中的关键信息即可

话不多说,直接上代码;

spider文件:
class BaidupictureSpider(scrapy.Spider):
    name = 'baidupicture'
    allowed_domains = ['http://image.baidu.com']

    #搜寻关键字列表
    search_lists = [‘臭豆腐’,‘鸡肉’,‘美女’]

    #关键字对应搜索的页数
    search_pages = [20,10,100]
    def start_requests(self):
        for i in range(len(self.search_lists)):
            queryWord = urlencode({'queryWord': self.search_lists[i]})
            word = urlencode({'word': self.search_lists[i]})
            for i in range(self.search_pages[i]):
                url ="https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&" + queryWord +"&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&"+word+"&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&pn=" + str(i*30) +"&rn=30"
                yield scrapy.Request(url, callback=self.parse, meta=({'search': self.search_lists[i]}),dont_filter=True)


    def parse(self, response):
        item = FacepictureItem()
        item['recode'] = response.meta['search']#关键字,可以传到pipeline作为文件名
        datas = response.text
        item['imgs_url'] = re.findall('"thumbURL":"(https://.*?.jpg)"', datas)#提取图片链接
        yield item

settings:

设置 

ROBOTSTXT_OBEY = False

pipeline:

from hashlib import md5
from urllib.request import urlretrieve
import os

class FacepicturePipeline(object):
    def process_item(self, item, spider):
        if not os.path.exists(item['recode']):
            os.mkdir(item['recode'])
        for url in item['imgs_url']:
            print('正在写的url是:', url)
            img_path = '{0}/{1}.{2}'.format(item['recode'], md5(url.encode("utf-8")).hexdigest(), 'jpg')
            try:
                if not os.path.exists(img_path):
                    urlretrieve(url, filename=img_path)
            except:
                continue
        return item

完毕

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值