remote_command

本文介绍了一种使用Python的requests库和BeautifulSoup库从百度贴吧抓取帖子内容的方法。通过设置请求头,发送GET请求获取网页源代码,再利用BeautifulSoup解析网页,找到并打印出特定类别的div元素,最后提取并输出所需的内容。
import requests
from bs4 import BeautifulSoup
import json
def get_response():
    URL = "https://tieba.baidu.com/p/6372327480"
    headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"}
    r = requests.get(URL,headers=headers)
    # print(r.text)
    soup = BeautifulSoup(r.text,'lxml')
    match = soup.find_all(name="div",attrs={"class":"d_post_content j_d_post_content"})
    print(match)
    print(match[-1])
    return str(match[-1])

def get_command(class_content):
    string1 = r'style="display:;">'
    string2 = r'</div>'
    content = class_content.split(string1)[-1].split(string2)[0].strip()
    print(content)
    return content

if __name__ == '__main__':
    print("="*20)
    class_content =get_response()
    content = get_command(class_content)
    command = json.loads(content)
    print(command)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值