import requests
from bs4 import BeautifulSoup #导入BeautifulSoup库
#由于豆瓣有反爬虫策略,headers用于伪装成浏览器访问,
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
}
#观察豆瓣top250每一页的网页,发现每次递增25,于是每次循环步长25,用来更新链接
for start in range(0,250,25):
response = requests.get(f"https://movie.douban.com/top250?start={start}",headers=headers) #更新链接
html = response.text #获取html文件,传递给html变量
suop = BeautifulSoup(html,"html.parser") #调用BeautifulSoup,用html.parser来解析html
titles = suop.findAll("span", attrs={"class": "title"}) #过滤,找到所有的span标签,class=title的信息
for tit in titles: #遍历titles给tit
tit_string = tit.string #只将tit的字符串提取出来赋值给tit——string
if "/" not in tit_string: #如果不带有“/”,就输出
print(tit_string)
01-20
1928
02-24
621
04-11
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交