使用BeautifulSoup 解析和提取网页中的数据
安装库 pip install BeautifulSoup4
import requests
from bs4 import BeautifulSoup #引入BS库
res=requests.get('http://www.zongheng.com/rank.html')
sp=BeautifulSoup(res.text,'html.parser') #括号中的第0个参数,必须是字符串类型;第1个参数是解析器
print(type(sp))
><class 'bs4.BeautifulSoup'>
利用bs解析数据后就可以利用bs中的方法来提取数据。
方法一: find()与find_all()
find()只提取首个满足要求的数据,而find_all()提取出的是所有满足要求的数据。
import requests
from bs4 import BeautifulSoup #引入BS库
res=requests.get('http://www.zongheng.com/rank.html')
sp=BeautifulSoup(res.text,'html.parser') #用BS解析数据,括号中的第0个参数,必须是字符串类型;第1个参数是解析器
tp=sp.find_all('div',class_="borderB_c_dsh")
print(type(tp))
><class 'bs4.element.ResultSet'> #相当于Tag对象以列表结构储存了起来,可以把它当做列表来处理
Tag对象
import requests
from bs4 import BeautifulSoup #引入BS库
res=requests.get('http://www.zongheng.com/rank.html')
sp=BeautifulSoup(res.text,'html.parser') #括号中的第0个参数,必须是字符串类型;第1个参数是解析器
tp=sp.find_all('div',class_="borderB_c_dsh")
for i in tp:
print(type(i))
><class 'bs4.element.Tag'> #把ResultSet中的数据提取出来就是一个个的tag对象
>....
import requests #调用requests库
from bs4 import BeautifulSoup #引入BS库
res=requests.get('http://www.zongheng.com/rank.html') #返回一个response对象,赋值给res
sp=BeautifulSoup(res.text,'html.parser') #括号中的第0个参数,必须是字符串类型;第1个参数是解析器。把网页解析为BS对象赋值给sp
tp=sp.find_all('div',class_="borderB_c_dsh") #通过匹配属性class="borderB_c_dsh'提取出我们想要的元素。class后面一定要接 _ 下横线符号。可能是为了便于区分。
for i in tp:
name=i.find('div',class_="rank_i_bname fl").find('a') #tag对象继续提取获得标签<a>里的值
h=name['href'] #获取标签<a>里属性为href的值
print(f'书名:{name.text}\n链接:{h}\n') #打印书籍名称和衔接
select用法
import requests #调用requests库
from bs4 import BeautifulSoup #引入BS库
res=requests.get('http://www.zongheng.com/rank.html') #返回一个response对象,赋值给res
sp=BeautifulSoup(res.text,'lxml') #括号中的第0个参数,必须是字符串类型;第1个参数是解析器。把网页解析为BS对象赋值给sp
#print(sp.select('.fl')) #获取所有class=f1的元素 ,前面要加点 . 前面加#号就代表id为什么的元素
#print(sp.select('.fl')[11].get_text()) #获取内容
name=sp.select('div.rank_i_bname ')
for i in name:
name=i.select('a')[0] #取第一条标签为a里的元素
print(name,name.string,name.attrs) #分别打印标签a的元素,取a中的值(string),取a中的标签元素
import requests
from bs4 import BeautifulSoup
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36'}
list_url=[]
for i in range(10):
url='https://movie.douban.com/top250?start='+str(i*25)+'&filter='
list_url.append(url)
print('豆瓣电影Top250排行榜\n')
for i in list_url:
res=requests.get(i,headers=headers)
bs=BeautifulSoup(res.text,'html.parser')
li=bs.find_all('div',class_="item")
for x in li:
xh=x.find('div',class_="pic").find('em').text
title=x.find('div',class_="hd").find('a').find('span').text
pf=x.find('div',class_="star").find('span',class_="rating_num").text
try:
tjy=x.find('p',class_="quote").find('span').text
except AttributeError:
tjy='没有推荐语'
lj=x.find('div',class_="hd").find('a')['href']
print(f"{xh}{title}\n评分{pf}\n推荐语:{tjy}\n链接{lj}\n")
import requests
from bs4 import BeautifulSoup
from urllib.request import quote
#quote()函数,可以帮我们把内容转为标准的url格式,作为网址的一部分打开
movie = input('你想看什么电影呀?')
gbkmovie = movie.encode('gbk')
#将汉字,用gbk格式编码,赋值给gbkmovie
url = 'http://s.ygdy8.com/plus/so.php?typeid=1&keyword='+quote(gbkmovie)
#将gbk格式的内容,转为url,然后和前半部分的网址拼接起来。
res = requests.get(url)
#下载××电影的搜索页面
res.encoding ='gbk'
#定义res的编码类型为gbk
soup_movie = BeautifulSoup(res.text,'html.parser')
#解析网页
urlpart = soup_movie.find(class_="co_content8").find_all('table')
# print(urlpart)
if urlpart:
urlpart = urlpart[0].find('a')['href']
urlmovie = 'https://www.ygdy8.com/' + urlpart
res1 = requests.get(urlmovie)
res1.encoding = 'gbk'
soup_movie1 = BeautifulSoup(res1.text,'html.parser')
urldownload = soup_movie1.find('div',id="Zoom").find('span').find('table').find('a')['href']
print(urldownload)
else:
print('没有' + movie)
# 有些电影是查询不到没下载链接的,因此加了个判断