利用XPath爬取百度贴吧指定内容

本文介绍了一种使用Python和XPath抓取百度贴吧回帖信息的方法,包括回帖人的用户名、回帖内容及回帖时间。通过多线程加速爬取过程,实现了对指定贴吧页面上所有回帖信息的有效抓取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是利用XPath爬取百度贴吧上的回帖人 回贴信息 回贴时间的代码

from lxml import etree
from multiprocessing.dummy import Pool as ThreadPool
import requests
import string
import json


def towrite(contentdict):
    f.writelines('回贴时间:' + str(contentdict['topic_reply_time']) + '\n')
    f.writelines('回贴内容:' + str(contentdict['topic_reply_content']) + '\n')
    f.writelines('回帖人:' + str(contentdict['user_name']) + '\n\n')

def spider(url):
    html = requests.get(url)
    selector = etree.HTML(html.text)     # 转换为可用xpath处理的对象
    content_field = selector.xpath('//div[@class="l_post j_l_post l_post_bright  "]')        # 获取每一条回贴内容
    item = {}
    for each in content_field:
        # json.loads可将json格式的数据解析成字典格式   reply_info代表回贴的信息
        reply_info = json.loads(each.xpath('@data-field')[0])

        # 这里用到了先抓到再抓小的原则 所以XPath路径不用加那个//之类的啦
        author = reply_info['author']['user_name']
        content = each.xpath('div[@class="d_post_content_main"]/div/cc/div[@class="d_post_content j_d_post_content  clearfix"]/text()')[0]
        content = str.strip(content)
        reply_time = reply_info['content']['date']
        print(content)
        print(reply_time)
        print(author)
        item['user_name'] = author
        item['topic_reply_content'] = content
        item['topic_reply_time'] = reply_time
        towrite(item)

if __name__=='__main__':
    pool = ThreadPool(8)
    f = open('content.txt','a',encoding='utf-8')
    page = []
    for i in range(1,31):
        newpage = 'http://tieba.baidu.com/p/3522395718?pn=' + str(i)
        page.append(newpage)

    result = pool.map(spider,page)
    pool.close()
    pool.join()
    f.close()

Result

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值