鱼C论坛

 找回密码
 立即注册
查看: 2000|回复: 4

豆瓣top250求救

[复制链接]
发表于 2020-12-21 23:30:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests
  2. import bs4
  3. import re

  4. def open_url(url):
  5.     #使用代理
  6.     #proxies = {"http":"127.0.0.1:1080","https":"127.0.0.1:1080"}
  7.     headers = {'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/57.0.2987.95 Safari/537.36'}
  8.     # res = requests.get (url, headers = headers, proxies = proxies)
  9.     res = requests,get(url, headers = headers)
  10.     return res

  11. def find_movies(res):
  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.         try:
  23.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  24.         except:
  25.             continue

  26.     result = []
  27.     length = len(movies)
  28.     for i in range (length):
  29.         result.append(movies[i] + ranks[i] + messages[i] + '\n')
  30.     return result

  31. #找出一共有多少个页面
  32. def find_depth(res):
  33.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  34.     depth = soup.find('span', class_='next').previous_sibling.previous_sibling.text
  35.     return int(depth)

  36. def main():
  37.     host = "http://movie.douban.com/top250"
  38.     res = open_url(host)
  39.     depth = find_depth(res)

  40.     result = []
  41.     for i in range (depth):
  42.         url = host + '/?start=' + str(25*i)
  43.         res = open_url(url)
  44.         result.append(find_movies(res))

  45.     with open ("豆瓣TOP250电影.txt", "w", encoding="utf-8") as f:
  46.         for each in result:
  47.             f.write(each)

  48. if_ _name_ _ == "_ _main_ _":
  49.     main()
复制代码


到底是咋回事,这个下面的问题

    if_ _name_ _ == "_ _main_ _":
             ^
SyntaxError: invalid syntax
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-22 09:51:32 | 显示全部楼层
兄嘚,SyntaxError 最最最最最常见的是你用了中文符号
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 10:43:16 | 显示全部楼层
if _ _name_ _ == "_ _main_ _":
if后面少了一个空格、
if __name__ == "__main__":
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 10:43:25 | 显示全部楼层
if __name__ == '__main__':
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-12-22 11:10:57 | 显示全部楼层
代码错误挺多
看一下源码
  1. import requests
  2. import bs4
  3. import re

  4. def open_url(url):
  5.     # 使用代理
  6.     # proxies = {"http": "127.0.0.1:1080", "https": "127.0.0.1:1080"}
  7.     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'}

  8.     # res = requests.get(url, headers=headers, proxies=proxies)
  9.     res = requests.get(url, headers=headers)

  10.     return res

  11. def find_movies(res):
  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')[1].strip() + each.p.text.split('\n')[2].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

  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.    
  54. if __name__ == "__main__":
  55.     main()
复制代码


第十行res = requests,get(url, headers = headers)改成res = requests.get(url, headers = headers)
第二十三行之后抄错行了
# 资料的部分和#评分的部分抄混了,代码逻辑就乱套了
第56行if_ _name_ _ == "_ _main_ _": 改为 if __name__ == "__main__":
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-30 00:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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