Beautiful Soup 4
学习python的第十一天(2020.04.16)
Beautiful Soup4(BS4)是Python的一个第三方库,用来从HTML和XML中提取数据。Beautiful Soup4在某些方面比XPath易懂,但是不如XPath简洁,而且由于它是使用Python开发的,因此速度比XPath慢。
BS4的安装
打开+号,输入这两个安装就好了,非常简单,就不多说了。
BS4的用法(爬取百度实时热搜榜)
代码如下
#coding:utf-8
#coding: GBK
import requests
from bs4 import BeautifulSoup
baidu_top=requests.get('http://top.baidu.com/buzz?b=1&fyb=PcReDianAla01').content.decode('GBK') #爬取网页
soup =BeautifulSoup(baidu_top,'lxml')
table =soup.find_all(class_='list-table')
title =soup.find_all(class_='list-title')
print("百度实时热搜:")
for a in title:
print(a.string)
运行结果
小小说明
BS4速度比XPath慢,但是它看上去比XPath更简单
今日就这样吧,学习,总没错的。