美文网首页
Python批量爬取百度贴吧数据

Python批量爬取百度贴吧数据

作者: 黑猫编程 | 来源:发表于2019-10-03 19:24 被阅读0次

分析百度贴吧url

kw=python作为字典传入,pn=0为第一页,第二页pn=50,第二页pn=100

先构造出前10页url

# -*- coding: utf-8 -*-
# @Time    : 2019/10/3 18:56
# @Author  : 币行者
# @Email   : xypip@qq.com
# @File    : test5.py

import requests

headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}

url_temp = "https://tieba.baidu.com/f?kw={}&ie=utf-8&pn={}"
url_list = [url_temp.format("python", i * 50) for i in range(10)]

print(url_list)

将获取到10页数据全部保存至本地

# -*- coding: utf-8 -*-
# @Time    : 2019/10/3 18:56
# @Author  : 币行者
# @Email   : xypip@qq.com
# @File    : test5.py

import requests

headers = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"}

url_temp = "https://tieba.baidu.com/f?kw={}&ie=utf-8&pn={}"
url_list = [url_temp.format("python", i * 50) for i in range(10)]

# print(url_list)

for url in url_list:
    response = requests.get(url, headers=headers)
    html_str = response.content.decode()
    page_num = url_list.index(url) + 1
    file_path = "{}—第{}页.html".format("python", page_num)
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(html_str)

面向对象方法

# -*- coding: utf-8 -*-
# @Time    : 2019/10/3 18:37
# @Author  : 币行者
# @Email   : xypip@qq.com
# @File    : baidutieba_spider.py

import requests

class TiebaSpider:

    def __init__(self, tieba_name, tieba_num):
        self.tieba_name = tieba_name
        self.tieba_num = tieba_num
        self.url_temp = "https://tieba.baidu.com/f?kw=" + tieba_name + "&ie=utf-8&pn={}"
        self.headers = {"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36"}

    def get_url_list(self):
        return [self.url_temp.format(i * 50) for i in range(self.tieba_num)]

    def parse_url(self, url):
        print(url)
        response = requests.get(url, headers=self.headers)
        return response.content.decode()

    def save_html(self, html_str, page_num):
        file_path = "{}—第{}页.html".format(self.tieba_name, page_num)
        with open(file_path, "w", encoding="utf-8") as f:
            f.write(html_str)

    def run(self):
        url_list = self.get_url_list()
        for url in url_list:
            html_str = self.parse_url(url)
            page_num = url_list.index(url) + 1  # 页码数
            self.save_html(html_str, page_num)



if __name__ == '__main__':

    tieba_spider = TiebaSpider("python", 10)
    tieba_spider.run()

相关文章

  • Python批量爬取百度贴吧数据

    分析百度贴吧url kw=python作为字典传入,pn=0为第一页,第二页pn=50,第二页pn=100 先构造...

  • 第四阶段 爬虫整理

    爬虫概述 爬虫案例 案例1:爬取百度贴吧数据 分析:GET方式爬取数据抓包:设计:实现: 案例2:抓妹子图 分析:...

  • 可视化pyecharts库初体验

    爬取学校贴吧150个帖子,统计词频,简单数据分析 一、数据采集目标站点:百度贴吧 二、分词统计词频(jieba) ...

  • python爬取百度贴吧

    爬取百度贴吧python文件源代码如下(欢迎点赞哦) import urllib.request import u...

  • python爬取百度贴吧

    爬取百度贴吧python文件源代码如下(欢迎点赞哦) import urllib.request import u...

  • Python爬虫实战

    注:采转归档,自己学习查询使用 Python爬虫实战(1):爬取糗事百科段子Python爬虫实战(2):百度贴吧帖...

  • python爬取百度贴吧的图片1

    python版本:2.7.10学习python爬虫,首先写了一个爬取百度贴吧图片的程序。参考了静觅的系列博客 好了...

  • python爬取百度贴吧

    1.对百度贴吧的任意帖子进行抓取 2.指定是否只抓取楼主发帖内容 3.将抓取到的内容分析并保存到文件

  • python爬虫之百度贴吧

    最近又尝试着爬取了百度贴吧,发现新增的几个反爬点,故来做下记录。 爬取百度贴吧大致流程为: 1 - 构造url,h...

  • 爬取百度贴吧帖子

    依然是参考教程 Python爬虫实战一之爬取百度贴吧帖子。作者崔庆才写了很多关于Python爬虫的文章,大家有兴趣...

网友评论

      本文标题:Python批量爬取百度贴吧数据

      本文链接:https://www.haomeiwen.com/subject/vradpctx.html