鱼C论坛

 找回密码
 立即注册
查看: 1763|回复: 8

PYTHON爬虫豆瓣250名代码出错

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

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

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

x
Traceback (most recent call last):
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\book lean\爬虫自我修养4 爬取豆瓣网评分.py", line 87, in <module>
    main()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\book lean\爬虫自我修养4 爬取豆瓣网评分.py", line 69, in main
    res = open_url(host)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\book lean\爬虫自我修养4 爬取豆瓣网评分.py", line 11, in open_url
    res = requests.get(url, headers=headers, proxies=proxies)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 412, in send
    conn = self.get_connection(request.url, proxies)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 309, in get_connection
    proxy_manager = self.proxy_manager_for(proxy)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 193, in proxy_manager_for
    manager = self.proxy_manager[proxy] = proxy_from_url(
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\poolmanager.py", line 470, in proxy_from_url
    return ProxyManager(proxy_url=url, **kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\poolmanager.py", line 420, in __init__
    raise ProxySchemeUnknown(proxy.scheme)
urllib3.exceptions.ProxySchemeUnknown: Not supported proxy scheme None


运行过程中出现这些问题  请小甲鱼帮助一下

问题描述

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

使用道具 举报

发表于 2020-2-12 17:22:24 | 显示全部楼层
把你的代码发上来!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-12 20:30:57 | 显示全部楼层
下载看看

056论一只爬虫的自我修养4:OOXX(源代码).zip

123.07 KB, 阅读权限: 80, 下载次数: 4

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

使用道具 举报

 楼主| 发表于 2020-2-13 10:27:29 | 显示全部楼层
本帖最后由 zltzlt 于 2020-2-13 13:14 编辑
zltzlt 发表于 2020-2-12 17:22
把你的代码发上来!

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

  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.     res = requests.get(url, headers=headers, proxies=proxies)
  10.     res = requests.get(url, headers=headers)

  11.     return res

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

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

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

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

  32.     result = []
  33.     length = len(movies)
  34.     for i in range(length):
  35.         result.append([movies[i], ranks[i], messages[i]])

  36.     return result

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

  41.     return int(depth)

  42. def save_to_excel(result):
  43.     wb = openpyxl.Workbook()
  44.     ws = wb.active

  45.     ws['A1'] = "电影名称"
  46.     ws['B1'] = "评分"
  47.     ws['C1'] = "资料"

  48.     for each in result:
  49.         ws.append(each)

  50.     wb.save("豆瓣TOP250电影.xlsx")

  51. def main():
  52.     host = "https://movie.douban.com/top250"
  53.     res = open_url(host)
  54.     depth = find_depth(res)

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

  60.     '''
  61.     with open("test.txt", "w", encoding="utf-8") as f:
  62.         for each in result:
  63.             f.write(each)
  64.     '''

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

使用道具 举报

发表于 2020-2-13 22:31:29 | 显示全部楼层

1.你使用的代理是有问题的
2.你写了两行res = requests.get(url, headers=headers, proxies=proxies)
                  res = requests.get(url, headers=headers)

当去掉了第一行的res = ...... 代码正常运行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-13 23:13:59 | 显示全部楼层
xiangzhihengkan 发表于 2020-2-13 22:31
1.你使用的代理是有问题的
2.你写了两行res = requests.get(url, headers=headers, proxies=proxies)
  ...

去掉第一行的res 一样报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 09:04:44 | 显示全部楼层
首虎 发表于 2020-2-13 23:13
去掉第一行的res 一样报错

复制你的代码,去掉以后是可以正常运行的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-14 15:37:27 | 显示全部楼层
xiangzhihengkan 发表于 2020-2-14 09:04
复制你的代码,去掉以后是可以正常运行的

你是哪个版本的python
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-14 15:41:25 | 显示全部楼层
首虎 发表于 2020-2-14 15:37
你是哪个版本的python

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-6 04:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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