鱼C论坛

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

[已解决]爬豆瓣TOP250失败

[复制链接]
发表于 2020-7-2 23:17:15 | 显示全部楼层 |阅读模式

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

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

x
这个是原代码,运行后没有报错,可是没有文件出来,不知道哪里错了。
  1. import requests
  2. import bs4
  3. import random
  4. import openpyxl

  5. def open_url(url):
  6.     '''
  7.     ip_poor = ['61.19.145.66:8080','105.27.237.27:80','110.36.239.234:8080'\
  8.            '110.39.187.50:49850','159.192.141.89:8080','162.243.244.206:80'\
  9.            '184.149.34.86:51529','181.118.167.104:80','3.6.99.110:80']
  10.     proxies = {'http':random.choice(ip_poor)}
  11.     '''        
  12.     headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'}
  13.    
  14.     #html = request.get(url=url, headers=headers, proxies=proxies)
  15.     html = request.get(url,headers=headers)
  16.    
  17.     return html

  18. def find_info(html):
  19.     content = bs4.BeautifulSoup(html.text,'html.parser')

  20.     #电影名
  21.     movie_name = []
  22.     target_name = content.find_all('div',class_='hd')
  23.     for each_name in target_name:
  24.         movie_name.append(each_name.a.span.text)

  25.     #评分
  26.     movie_score = []
  27.     target_score = content.find_all('span',class_='rating5-t')
  28.     for each_score in target_score:
  29.         movie_score.append(each_score.text)

  30.     #信息
  31.     movie_info = []
  32.     target_info = content.find_all('div',class_='bd')
  33.     for each_info in target_score:
  34.         try:
  35.             movie_info.append(each_info.p.text.split('\n')[1].strip() + each_info.p.text.split('\n')[2].strip())
  36.         except:
  37.             continue

  38.     content = []
  39.     for i in range(len(movie_name)):
  40.         result.append(movie_name[i] + movie_score[i] + movie_info[i])

  41.     return contenr

  42. def next_page(html):
  43.     target = bs4.BeautifulSoup(html.text,'html.parser')
  44.     next_path = target.find_all('span',class_='next').previous_sibling.previous_sibling.text
  45.     return int(next_path)

  46. def save_excel(content):
  47.     wb = openpyxl.Workbook()
  48.     ws = wb.active
  49.     ws['A1'] = '电影名'
  50.     ws['B1'] = '评分'
  51.     ws['C1'] = '电影信息'
  52.     for each in content:
  53.         ws.append(each)
  54.     wb.save(r'D:\Desktop\豆瓣TOP250电影.xlsx')

  55. def main():
  56.     url = 'https://movie.douban.com/top250'
  57.     html = open_url(url)
  58.     next_path = next_path(html)

  59.     content = []
  60.     for i in range(next_path):
  61.         url_next = url + '/?start=' + str(25 * i)
  62.         html = open_url(url_next)
  63.         content.extend(find_info(html))

  64.     save_excel(content)

  65. if __name__ == 'main':
  66.     main()
复制代码
最佳答案
2020-7-2 23:30:10
本帖最后由 Twilight6 于 2020-7-2 23:32 编辑




第16行使用 requests 模块少了个 s
错误:html = request.get(url,headers=headers)
正确:html = requests.get(url,headers=headers)


第 44 行~ 46 行,result 不知从何而来,应该是你不小心写成了 content = []
错误:
  1. content = []
  2.     for i in range(len(movie_name)):
  3.         result.append(movie_name[i] + movie_score[i] + movie_info[i])
  4. return contenr
复制代码

正确:
  1. result = []
  2.     for i in range(len(movie_name)):
  3.         result.append(movie_name[i] + movie_score[i] + movie_info[i])
  4. return result
复制代码


第 68 行,next_page 写成了 next_path:
错误:next_path = next_path(html)
正确:next_path = next_page(html)


暂时就找到这么多错误,是否运行成功我还没看


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

使用道具 举报

发表于 2020-7-2 23:30:10 | 显示全部楼层    本楼为最佳答案   
本帖最后由 Twilight6 于 2020-7-2 23:32 编辑




第16行使用 requests 模块少了个 s
错误:html = request.get(url,headers=headers)
正确:html = requests.get(url,headers=headers)


第 44 行~ 46 行,result 不知从何而来,应该是你不小心写成了 content = []
错误:
  1. content = []
  2.     for i in range(len(movie_name)):
  3.         result.append(movie_name[i] + movie_score[i] + movie_info[i])
  4. return contenr
复制代码

正确:
  1. result = []
  2.     for i in range(len(movie_name)):
  3.         result.append(movie_name[i] + movie_score[i] + movie_info[i])
  4. return result
复制代码


第 68 行,next_page 写成了 next_path:
错误:next_path = next_path(html)
正确:next_path = next_page(html)


暂时就找到这么多错误,是否运行成功我还没看


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

使用道具 举报

 楼主| 发表于 2020-7-2 23:33:13 | 显示全部楼层
Twilight6 发表于 2020-7-2 23:30
第16行使用 requests 模块少了个 s

是我太粗心了,看来要细心一点才行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-7-2 23:48:16 | 显示全部楼层
Twilight6 发表于 2020-7-2 23:30
第16行使用 requests 模块少了个 s

虽然错误排掉了,可是还是运行失败
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-7-3 00:01:59 | 显示全部楼层
changmind 发表于 2020-7-2 23:48
虽然错误排掉了,可是还是运行失败



之前的存稿,也是看着视频和小甲鱼打的,你拿去对照下吧:

  1. import requests
  2. import bs4
  3. import openpyxl


  4. def open_url(url):

  5.     headers = {
  6.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}

  7.     res = requests.get(url, headers=headers)

  8.     return res


  9. def find_movies(res):
  10.     soup = bs4.BeautifulSoup(res.text, 'html.parser')

  11.     # 电影名
  12.     movies = []
  13.     targets = soup.find_all("div", class_="hd")
  14.     for each in targets:
  15.         movies.append(each.a.span.text)

  16.     # 评分
  17.     ranks = []
  18.     targets = soup.find_all("span", class_="rating_num")
  19.     for each in targets:
  20.         ranks.append(each.text)

  21.     # 资料
  22.     messages = []
  23.     targets = soup.find_all("div", class_="bd")
  24.     for each in targets:
  25.         try:
  26.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  27.         except:
  28.             continue

  29.     result = []
  30.     length = len(movies)
  31.     for i in range(length):
  32.         result.append([movies[i] ,ranks[i] ,messages[i]])

  33.     return result


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

  38.     return int(depth)

  39. def save_to_excel(result):
  40.     wb = openpyxl.Workbook()
  41.     ws = wb.active

  42.     ws['A1'] = '电影名称'
  43.     ws['B1'] = '评分'
  44.     ws['C1'] = '资料'

  45.     for each in result:
  46.         ws.append(each)

  47.     wb.save('豆瓣TOP250.xlsx')


  48. def main():
  49.     host = "https://movie.douban.com/top250"
  50.     res = open_url(host)
  51.     depth = find_depth(res)

  52.     result = []
  53.     for i in range(depth):
  54.         url = host + '/?start=' + str(25 * i)
  55.         res = open_url(url)
  56.         result.extend(find_movies(res))
  57.     save_to_excel(result)


  58. if __name__ == "__main__":
  59.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-23 00:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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