【转】[爬虫] 爬取豆瓣TOP250电影排行榜

 

原文链接:

https://fishc.com.cn/thread-94979-1-1.html

  1. import requests
  2. import bs4
  3. import re
  4.  
  5. def open_url(url):
  6.     # 使用代理
  7.     # proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
  8.     headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
  9.  
  10.     # res = requests.get(url, headers=headers, proxies=proxies)
  11.     res = requests.get(url, headers=headers)
  12.  
  13.     return res
  14.  
  15. def find_movies(res):
  16.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  17.  
  18.     # 电影名
  19.     movies = []
  20.     targets = soup.find_all("div", class_="hd")
  21.     for each in targets:
  22.         movies.append(each.a.span.text)
  23.  
  24.     # 评分
  25.     ranks = []
  26.     targets = soup.find_all("span", class_="rating_num")
  27.     for each in targets:
  28.         ranks.append(' 评分:%s ' % each.text)
  29.  
  30.     # 资料
  31.     messages = []
  32.     targets = soup.find_all("div", class_="bd")
  33.     for each in targets:
  34.         try:
  35.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  36.         except:
  37.             continue
  38.  
  39.     result = []
  40.     length = len(movies)
  41.     for i in range(length):
  42.         result.append(movies[i] + ranks[i] + messages[i] + '\n')
  43.  
  44.     return result
  45.  
  46. # 找出一共有多少个页面
  47. def find_depth(res):
  48.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  49.     depth = soup.find('span', class_='next').previous_sibling.previous_sibling.text
  50.  
  51.     return int(depth)
  52.  
  53. def main():
  54.     host = "https://movie.douban.com/top250"
  55.     res = open_url(host)
  56.     depth = find_depth(res)
  57.  
  58.     result = []
  59.     for i in range(depth):
  60.         url = host + '/?start=' + str(25 * i)
  61.         res = open_url(url)
  62.         result.extend(find_movies(res))
  63.  
  64.     with open("豆瓣TOP250电影.txt", "w", encoding="utf-8") as f:
  65.         for each in result:
  66.             f.write(each)
  67.     
  68. if __name__ == "__main__":
  69.     main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值