爬取任意百度贴吧评论(可直接Copy)

代码拿去就可以用,输入你要爬的贴吧名就行了。

看贴吧有多少页,range范围改为多少

爬取的评论在D:/res/info.txt中,注意控制访问频率,不要把time.sleep()删了,要不然ip会被被临时封了,也可能被验证码疯狂骚扰,如果你有ip代理池那随意了

import requests
import os
import numpy as np
from concurrent.futures import ThreadPoolExecutor
import time
import re
header={
'user-agent':
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
}

#with open('D:/res/headers.txt','r',encoding='utf-8')as f:
#    headers=f.readlines()

#proxies=[]
#with open('D:/res/https.txt','r',encoding='utf-8') as f:
#    proxies=f.readlines()
proxy={'http':'http://106.11.226.232:8009','https':'http://106.11.226.232:8009'}
def fun(url,page):
    time.sleep(10)
    #proxy={'https':'http://'+np.random.choice(proxies),'http':'http://'+np.random.choice(proxies)}
    r=requests.get(url,headers=header,timeout=10,proxies=proxy,verify=False,allow_redirects=False)
    if r.status_code!=200:
        print('error')
    print(r.text)
    hrefs=re.findall('href=".*?" title=".*?" target="_blank" class="j_th_tit ">',r.text)
    print(hrefs)
    print(page)
    if len(hrefs)==0:
        return
    
    for href in hrefs:
        time.sleep(5)

        href='https://tieba.baidu.com'+href[6:19]

        #proxy={'https':'http://'+np.random.choice(proxies),'http':'http://'+np.random.choice(proxies)}
        r=requests.get(href,headers=header,timeout=10,proxies=proxy,verify=False,allow_redirects=False)

        print(r.encoding)
        r.encoding='utf-8'
        print(page)
        texts=re.findall('class="d_post_content j_d_post_content  clearfix" style="display:;">            .*?<',r.text)
        print(texts)
        prefix='class="d_post_content j_d_post_content  clearfix" style="display:;">            '
        for i in range(len(texts)):
            texts[i]=texts[i][len(prefix):-1]
        
        if not os.path.exists('D:/res'):
            os.makedirs('D:/res')
        
        with open('D:/res/info.txt','a',encoding='utf-8') as f:
            
            f.write(' '.join(texts))
                       

#pool=ThreadPoolExecutor(2)
#500

topic=input()
for page in range(0,1000):
    url=f'https://tieba.baidu.com/f?kw={topic}&ie=utf-8&pn={page*50}'
    #pool.submit(fun,url)
    fun(url,page)
    

爬取微博评论需要进行以下几个步骤: 1. 登录微博并进入需要爬取评论的页面,例如某一条微博的页面。 2. 打开浏览器的开发者工具(一般按 F12 键),并在 Network 标签页下刷新页面。 3. 点击页面上的“查看更多评论”按钮,此时 Network 标签页下会出现一个新的请求,这个请求的 URL 就是我们需要爬取评论的地址。 4. 右键点击请求,选择 Copy > Copy as cURL,得到请求的 cURL 命令,然后将其转换为 Python 代码。 5. 发送请求,解析返回的数据,获取评论内容。 下面是一个简单的 Python 爬取微博评论的示例代码: ```python import requests from bs4 import BeautifulSoup # 请求的地址,根据实际情况修改 url = 'https://weibo.com/1234567890/ABCDE' # 请求头部信息,根据实际情况修改 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 'Cookie': 'your cookie' } # 发送 GET 请求 response = requests.get(url, headers=headers) # 解析 HTML 页面,获取评论列表 soup = BeautifulSoup(response.text, 'html.parser') comment_list = soup.select('.list_li') # 输出评论内容 for comment in comment_list: print(comment.select('.WB_text')[0].text) ``` 需要注意的是,爬取微博评论需要登录,并且需要使用 Cookie 来维持登录状态。在代码中,将 Cookie 放在了请求头部信息中。另外,由于微博的页面结构经常变化,建议在实际使用时根据情况进行调整。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

装B且挨揍の

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值