百度贴吧图片爬取

其实没有用到header,用了反而什么也没反应,可能是一种反爬措施吧。。 

#图片爬虫
import urllib
import urllib.request
from lxml import etree

class Spider(object):
    def __init__(self):
        self.tiebaName ="女明星"
        self.beginPage =1
        self.endPage   =3
        self.url       ="http://tieba.baidu.com/f?"
        self.ua_header = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)\
	        AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0\
	        .2743.116 Safari/537.36',
	        'Accept-Language': 'zh-CN,zh;q=0.8'
        }
        self.fileName  = 0

    # 构造url
    def tiebaSpider(self) :
        word_dict   = {'kw':self.tiebaName}
        word_encode = urllib.parse.urlencode(word_dict)
        word_url    = self.url + word_encode + '&ie=utf-8&tab=album'
        self.loadPage(word_url)
        
    # 爬取页面内容
    def loadPage(self,url):
        request_info = urllib.request.Request(url)
        read_data    = urllib.request.urlopen(request_info).read()
        html         = etree.HTML(read_data)
        links_half   = html.xpath('//div[@class="gr_block_main"]/div/div/a/@href')
        
        for link in links_half:
            link_full = 'http://tieba.baidu.com' + link
            self.loadImages(link_full)
    
    # 爬取帖子详情页,获得图片的链接
    def loadImages(self,link_full):
        request_info = urllib.request.Request(link_full)
        read_data    = urllib.request.urlopen(request_info).read()
        html         = etree.HTML(read_data)
        links        = html.xpath('//img/@src')

        for image_link in links:
            condition = 'http'
            if condition in image_link:
                self.writeImages(image_link)
            else:
                continue

    #通过图片所在链接,爬取图片并保存图片到本地:
    def writeImages(self,image_link):
        image = urllib.request.urlopen(image_link).read()
        # 保存图片到本地
        file  = open(r"E:\picture\\"+str(self.fileName)+".jpg","wb")
        file.write(image)
        file.close()
        self.fileName+=1
        print("downloading......",self.fileName)
        
if __name__ == '__main__':
    mySpider=Spider()
    mySpider.tiebaSpider()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值