鱼C论坛

 找回密码
 立即注册
查看: 1393|回复: 2

爬虫ConnectionError问题

[复制链接]
发表于 2020-8-11 17:00:06 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 937135952 于 2020-8-11 17:28 编辑

部分代码如下
  1. import requests  
  2. from bs4 import BeautifulSoup
  3. from lxml import etree
  4. from cnsenti import Sentiment
  5. import jieba
  6. import smtplib
  7. from email.mime.text import MIMEText
  8. import os
  9. import time


  10. def get_and_save(url):
  11.     global save_txt
  12.     headers = {
  13.     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  14.     'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  15.     'Accept-Language':'en-US,en;q=0.5',
  16.     'Accept-Encoding':'gzip',
  17.     'DNT':'1',
  18.     'Connection':'close'
  19.     }

  20.    
  21.    
  22.     r = requests.get(url, headers=headers)
  23.     r.encoding = 'utf-8'
  24.     html = etree.HTML(r.text)#etree.HTML():构造了一个XPath解析对象并对HTML文本进行自动修正。
  25.     result = html.xpath('//div[contains(@id,"zw_body")]/p/text()')#('//div[@id="mainNewsContent"]/p/text()')

  26. #处理文本   
  27.     result = str(result)
  28.     result2 = result.replace('\\u3000','')
  29.     #print(result2)

  30.    

  31. def get_url():

  32.     #xunhuan=[1]
  33.     xunhuan=[1,2,3,4,5,6,7,8,9]
  34.     liebiao=[]
  35.    
  36.     for k in xunhuan:
  37.         print (k)
  38.         html = 'http://guba.eastmoney.com/default,1_'+str(k)+'.html'
  39.    
  40.         headers = {
  41.         'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36',
  42.         'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  43.         'Accept-Language':'en-US,en;q=0.5',
  44.         'Accept-Encoding':'gzip',
  45.         'DNT':'1',
  46.         'Connection':'close'
  47.         }
  48.    
  49.         page = requests.get(html,headers=headers)
  50.         
  51.         soup_obj=BeautifulSoup(page.content,'html.parser')
  52.         
  53.         #print(soup_obj.prettify())
  54.         
  55.         for link in soup_obj.findAll('a'):#含a
  56.             if "href" in link.attrs:#且以href作为特征..href在link的特征里
  57.                 #print(link.attrs['href'])
  58.                
  59.                 a = 'http://guba.eastmoney.com'+link.attrs['href']#href=‘/news,000762,954300722.html’
  60.                
  61.                
  62.                 if 'news' in a:

  63.                     liebiao.append(a)                     

  64.            
  65.     for i in liebiao:      

  66.         get_and_save(i)               
  67.         print("——————————————————————————————————————————————————————————————————————————执行了"+i)      


  68. if __name__ =='__main__':
  69.     get_url()
  70.    
复制代码



错误:ConnectionError: HTTPConnectionPool(host='guba.eastmoney.comnews,cjpl,953932572.html', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001644FDBB948>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))

一开始可以爬,爬了一会儿就会出现这个错误。首先我以为是访问过于频繁,加了time.sleep(60),同样爬一会儿会出现这个错误。
加入异常处理,爬一会儿会出现这个错误,连着好几个都是这个错,接着又能正常爬取,接着又是连着好几个这个错,如此反复。

出现这个错误的原因是服务器把我屏蔽了吗?还是什么原因?有什么解决办法吗?



还有一个问题...是我爬多了被服务器施加了什么神奇的魔法吗,打开链接跳转到的都是同一个内容....不管是手动还是用代码打开链接
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-8-12 09:14:00 | 显示全部楼层
没人了么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-13 08:27:44 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-25 15:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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