鱼C论坛

 找回密码
 立即注册
查看: 1321|回复: 12

《零基础》(第二版)的豆瓣爬虫代码问题

[复制链接]
发表于 2019-7-26 17:17:02 | 显示全部楼层 |阅读模式

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

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

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

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

  10. def find_movies(res):
  11.     soup = bs4.BeautifulSoup(res.text,'html.parser')
  12.     movies = []
  13.     targets = soup.find_all('div',class_='hd')
  14.     for each in targets:
  15.         movies.append(each.a.span.text)
  16.     ranks = []
  17.     targets = soup.find_all('span',class_='rating_num')
  18.     for each in targets:
  19.         ranks.append('评分: %s' % each.text)
  20.     messages = []
  21.     targets = soup.find_all('div',class_='bd')
  22.     for each in targets:
  23.         try:
  24.             messages.append(each.p.text.split('\n')[1].strip() + each.p.text.split('\n')[2].strip())
  25.         except:
  26.             continue

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

  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 = 'https://movies.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.extend(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()
复制代码



这是书上我原本照抄下来的代码 运行没有报错 中间应该没有低级错误 但是运行不报错 但是没有文件生成
麻烦各位大佬看一看我哪里我不问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-7-26 17:19:03 | 显示全部楼层
我有这样的疑问 是因为小甲鱼 在后面的网易云音乐的爬虫代码中有一个太明显的错误了 所以我才怀疑是不是这一段中有没有什么错误

本人就对计算机真的零基础

这一段还是要求有网页的基本常识还有计算机的常识

但是我都不清楚 所以这一章额外吃力 基本没自己敲过代码 都是抄的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-26 17:29:13 | 显示全部楼层
盼着各路神仙来指导一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-26 18:08:16 | 显示全部楼层
我寻思着,你的缩进有错吧。

56,57行往前挪
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-26 20:35:58 | 显示全部楼层
新手·ing 发表于 2019-7-26 18:08
我寻思着,你的缩进有错吧。

56,57行往前挪

版主 我改完过后 出现了。。。一段报错 懵逼了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-26 20:36:23 | 显示全部楼层
哈哈怪 发表于 2019-7-26 20:35
版主 我改完过后 出现了。。。一段报错 懵逼了

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

使用道具 举报

 楼主| 发表于 2019-7-26 20:36:33 | 显示全部楼层
Traceback (most recent call last):
  File "F:\python\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "F:\python\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection
    raise err
  File "F:\python\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\python\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen
    self._prepare_proxy(conn)
  File "F:\python\lib\site-packages\urllib3\connectionpool.py", line 807, in _prepare_proxy
    conn.connect()
  File "F:\python\lib\site-packages\urllib3\connection.py", line 316, in connect
    conn = self._new_conn()
  File "F:\python\lib\site-packages\urllib3\connection.py", line 169, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x000000000372A358>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "F:\python\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "F:\python\lib\site-packages\urllib3\connectionpool.py", line 641, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "F:\python\lib\site-packages\urllib3\util\retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='movies.douban.com', port=443): Max retries exceeded with url: /top250 (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x000000000372A358>: Failed to establish a new connection: [WinError 10061] 由于目标计算机积极拒绝,无法连接。')))

During handling of the above exception, another exception occurred:
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-26 20:37:05 | 显示全部楼层
哈哈怪 发表于 2019-7-26 20:36
Traceback (most recent call last):
  File "F:\python\lib\site-packages%urllib3\connection.py", line ...

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

使用道具 举报

 楼主| 发表于 2019-7-26 20:37:06 | 显示全部楼层
这个是报错的内容 请问这个内容是什么鬼 里面居然还有中文诶
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-26 20:42:02 | 显示全部楼层
哈哈怪 发表于 2019-7-26 20:37
这个是报错的内容 请问这个内容是什么鬼 里面居然还有中文诶

简单地说就是人家知道你是爬虫,你要换个代理
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-7-27 09:35:06 | 显示全部楼层
那不用代理可以吗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-7-27 09:37:40 | 显示全部楼层
哈哈怪 发表于 2019-7-27 09:35
那不用代理可以吗

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

使用道具 举报

 楼主| 发表于 2019-7-27 16:45:31 | 显示全部楼层

那问一下 现在还有代理能用吗 我直接百度搜的代理 还是会出现这样的问题

还有就是请教一下 为什么一定要用代理
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2026-1-17 04:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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