datawhale爬虫task-2之BeautifulSoup实战

2.1 学习beautifulsoup

  1. 学习beautifulsoup,并使用beautifulsoup提取内容。
  1. 使用beautifulsoup提取丁香园论坛的回复内容。
  1. 丁香园直通点:http://www.dxy.cn/bbs/thread/626626#626626
  1. 参考资料:https://blog.csdn.net/wwq114/article/details/88085875

 

使用beautifulsoup提取丁香园论坛的回复内容

1, Beautiful Soup的简介

beautifulsoup官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

Beautiful Soup将复杂HTML文档转换成一个复杂的树形结构,每个节点都是Python对象,所有对象可以归纳为4种: Tag , NavigableString , BeautifulSoup , Comment .

2. Beautiful Soup 安装

我使用的是anaconda python发行版本,已经包含bs4,所以无需安装

3. 创建 Beautiful Soup 对象

导入BeautifulSoup模块:

from bs4 import BeautifulSoup

创建 beautifulsoup 对象

html=BeautifulSoup(exanmple.html,‘lxml’)

4,获取获取所有包含用户名和评论内容的tbody

html.find_all("tbody")

5,分别获取用户名和评论内容

 

userid = data.find("div", class_="auth").get_text(strip=True)
content = data.find("td", class_="postbody").get_text(strip=True)

 

源码:

import urllib.request
from bs4 import BeautifulSoup as bs
def main():
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
                          "Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0"
    }
    url = 'http://www.dxy.cn/bbs/thread/626626'
    request = urllib.request.Request(url, headers=headers)
    response = urllib.request.urlopen(request).read().decode("utf-8")
    html = bs(response, 'lxml')
    getItem(html)
def getItem(html):
    datas = [] # 用来存放获取的用户名和评论
    for data in html.find_all("tbody"):
        try:
            userid = data.find("div", class_="auth").get_text(strip=True)
            print(userid)
            content = data.find("td", class_="postbody").get_text(strip=True)
            print(content)
            datas.append((userid,content))
        except:
            pass
    print(datas)



if __name__ == '__main__':
    main()

输出结果:

参考:

Python中使用Beautiful Soup库的超详细教程_python_脚本之家 https://www.jb51.net/article/65287.htm

python爬虫初步之BeautifulSoup实战 - wwq114的博客 - CSDN博客 https://blog.csdn.net/wwq114/article/details/88085875

Beautiful Soup 4.2.0 documentation   https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值