人间小甜饼281 2021-08-26 21:59 采纳率: 100%
浏览 78
已结题

微信爬虫失败原因:由于计算机积极拒绝

新手学了微信文章爬虫
参考了前辈们的代码,确不能成功,求助各位大佬!
如下:
# 搜狗微信搜索_订阅号及文章内容独家收录,一搜即达 微信公众号,精彩内容独家收录,一搜即达 http://weixin.sogou.com/
import re
import urllib.request
import time
import urllib.error
import urllib.request

import scipy
#自定义函数,功能为使用代理服务器爬一个网址
def use_proxy(proxy_addr,url):
#建立异常处理机制
try:
req=urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36')
proxy= urllib.request.ProxyHandler({'http':proxy_addr})
opener = urllib.request.build_opener(proxy, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
data = urllib.request.urlopen(req).read()
return data
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
#若为URLError异常,延时10秒执行
time.sleep(10)
except Exception as e:
print("exception:"+str(e))
#若为Exception异常,延时1秒执行
time.sleep(1)

#设置关键词
key="Python"
#设置代理服务器,该代理服务器有可能失效,读者需要换成新的有效代理服务器
proxy="120.76.231.27:3128"
#爬前10页
for i in range(1,10):
key=urllib.request.quote(key)
thispageurl= ' ' key '的相关微信公众号文章 – 搜狗微信搜索 http://weixin.sogou.com/weixin?query=%27+key+%27&_sug_type_=&sut=4983&lkt=10%2C1527762297236%2C1527762302210&s_from=input&_sug_=y&type=2&sst0=1527762302313&page=%27+str(i)+%27&ie=utf8&w=01019900&dr=1%27
print(thispageurl)
thispagedata=use_proxy(proxy,thispageurl)
print(len(str(thispagedata)))

pat1='<a href="(.*?)"'
rs1=re.compile(pat1,re.S).findall(str(thispagedata))
if(len(rs1)==0):
    print("此次("+str(i)+"页)没成功")
    continue
for  j in range(0,len(rs1)):
    thisurl=rs1[j]
    thisurl=thisurl.replace("amp;","")
    print(thisurl)
    file="D:/Python学习/第"+str(i)+"页第"+str(j)+"篇文章.html"
    thisdata=use_proxy(proxy,thisurl)
    try:
        fh=open(file,"wb")
        fh.write(thisdata)
        fh.close()
        print("第"+str(i)+"页第"+str(j)+"篇文章成功")
    except Exception as e:
        print(e)
        print("第"+str(i)+"页第"+str(j)+"篇文章失败")

出现以下错误:

[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
4
此次(1页)没成功
[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
4
此次(2页)没成功
[WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
4
自己尝试解决方法:
1.换了数个代理ip——还是失败
2.尝试允许应用通过防火墙——还是不行
3.加上decode
data = urllib.request.urlopen(req).read().decode("utf-8", "ignore")
还是失败

求助各位大佬

  • 写回答

2条回答 默认 最新

  • CSDN专家-HGJ 2021-08-26 23:07
    关注

    问题是在这句,proxy= urllib.request.ProxyHandler({'http':proxy_addr}),设置代理语句写法错误,改写成如下即可:

    proxy= urllib.request.ProxyHandler({'http://':proxy_addr})
    

    如对你有帮助,请点击我回答的右上方采纳按钮给予采纳。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 9月4日
  • 已采纳回答 8月27日
  • 修改了问题 8月26日
  • 创建了问题 8月26日

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格