鱼C论坛

 找回密码
 立即注册
查看: 4805|回复: 5

小甲鱼爬豆瓣TOP250有个代码看不懂

[复制链接]
发表于 2022-3-24 23:01:05 | 显示全部楼层 |阅读模式
10鱼币
  1. import requests
  2. import bs4
  3. import re

  4. def open_url(url):
  5.     #使用代理
  6.     # proxies = {'https':'223.243.174.152:9999','https':'223.214.205.179:9999'}
  7.     headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46'}
  8.     res = requests.get(url,headers=headers)
  9.     return res

  10. def find_movies(res):
  11.     #炖汤
  12.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  13.     #电影名
  14.     movies = []
  15.     targets = soup.find_all('div',class_ = 'hd')
  16.     for each in targets:
  17.         movies.append(each.a.span.text)

  18.     #评分
  19.     ranks = []
  20.     targets = soup.find_all('span',class_='rating_num')
  21.     for each in targets:
  22.         ranks.append('评分%s'%each.text)
  23.     #资料
  24.     messages = []
  25.     targets = soup.find_all('div',class_='bd')
  26.     for each in targets:
  27.         try:
  28.             messages.append(each.p.text.split('\n')[0].strip() + each.p.text.split('\n')[1].strip())
  29.         except:
  30.             continue

  31.     result = []
  32.     length = len(movies)
  33.     for i in range(length):
  34.         result.append(movies[i] + ranks[i] + messages[i] +'\n')
  35.     return result

  36. #找出一共有多少个页面
  37. def find_depth(res):
  38.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  39.     depth = soup.find('span',class_= 'next').previous_sibling.previous_sibling.text #这一行什么意思呀? 屏幕截图 2022-03-24 230037.png
  40.     return int(depth)

  41. def main():
  42.     host = 'https://movie.douban.com/top250'
  43.     res = open_url(host)
  44.     depth = find_depth(res)

  45.     result = []
  46.     for i in range(depth):
  47.         url = host + '/?start='+str(25*i)
  48.         res =open_url(url)
  49.         result.extend(find_movies(res))

  50.     with open('豆瓣TOP250.txt','w',encoding='utf-8') as f:
  51.         for each in result:
  52.             f.write(each)

  53. if __name__ == '__main__':
  54.     main()
复制代码


小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-24 23:02:57 | 显示全部楼层
我知道previous_sibling是找之前的同级标签,可是我查看了html代码还是只有个大体的猜测,需要大佬们的指导下.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-24 23:05:27 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-25 16:34:34 | 显示全部楼层
isdkz 发表于 2022-3-24 23:05
https://blog.csdn.net/Winterto1990/article/details/47794941

可是我看使用两次previous_sibling找到的不应该是9号标签吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-26 21:58:43 | 显示全部楼层
就是span节点的上一个兄弟节点的上一个兄弟节点,这么说你看你有点乱,我举一个例子吧
  1. In [12]: text = "<div><a>text0</a><b>text1</b><c>text2</c></div>"
  2. In [13]: soup = bs4.BeautifulSoup(text, "html.parser")
  3. In [14]: soup.find("c").previous_sibling.previous_sibling.text
  4. Out[14]: 'text0'
复制代码

c的上上个兄弟节点就是a,所以拿到的文本是text0
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-27 08:34:15 | 显示全部楼层
啥子东东啊??
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-16 23:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表