【Python】百度贴吧图片的爬虫实现(努力努力再努力)

学会爬取图片以后,第一时间去了张艺兴吧,哈哈哈哈哈哈

一定要放上一张爬取的照片,哼唧

import re

import requests

import urllib

class Baidutieba():

    def __init__(self):
        self.url = "http://tieba.baidu.com/p/4876047826?pn={}"#url地址
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"}

    def parse_url(self,url):
        response = requests.get(url,headers = self.headers)
        return response.content.decode("utf-8")

    def get_image(self,html_str):
        reg = r'src="(http://img.*?\.jpg)"'
        imgre = re.compile(reg)
        image_list = re.findall(reg, html_str)
        print(image_list)
        return image_list

    def download(self,image_list):
        x=1
        for each in image_list:
            print(x)
            print(each)
            urllib.request.urlretrieve(each,"%s.jpg"%x)
            x+=1

    def run(self):
        #1.构造url_list
        url_list = [self.url.format(i) for i in range(1,3)]#页数
        for url in url_list:
            # 2.发送请求,获取响应
            html_str = self.parse_url(url)
            #3.提取数据
            image_list = self.get_image(html_str)
            #4.下载到本地
            self.download(image_list)

if __name__ == '__main__':
    tieba = Baidutieba()
    tieba.run()

参考页面:貌似这两个都是python2的环境 

https://blog.csdn.net/qq_24421591/article/details/52596076

https://blog.csdn.net/z49434574/article/details/51552088

猜你喜欢

转载自blog.csdn.net/csdn___csdn/article/details/81208974