爬取到的data不是想要获取文章页面的源码,而是跳转到验证码的页面的源码。我网上查了一下是由于访问过于频繁导致的,我也加了time.sleep和改了请求头但还是显示不了,求大神支招,除了识别验证码的方式还能怎么做??

import re
import urllib.request
import time
import urllib.error headers = {'User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
opener = urllib.request.build_opener()
opener.addheaders = [headers]
#设置一个列表listurl储存文章网址列表
listurl = []
#函数:使用代理IP
def use_proxy(proxy_addr,url):
try:
import urllib.request
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(url).read().decode('utf-8')
return data
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"e.reason"):
print(e.reason)
time.sleep(10)
except Exception as e:
print("expection:"+str(e))
time.sleep(1) #函数:获取所有文章的连接
def getlisturl(key,pagestart,pageend,proxy):
try:
#page = pagestart
#编码关键词
keycode = urllib.request.quote(key)
#编码"&page"
#pagecode = urllib.request.quote("&page")
#循环爬取各页的文章链接
for page in range(pagestart,pageend+1,1):
#每次循环构建各页的url
url = 'http://weixin.sogou.com/weixin?&type=2&ie=utf8&query='+keycode+"&&page="+str(page)
#用换IP函数获得data
data1 = use_proxy(proxy,url)
time.sleep(1)
#匹配的正则表达式
pattern1 = '<div class="txt-box">.*?(http://.*?)"'
listurl.append(re.compile(pattern1,re.S).findall(data1))
print("共获取到"+str(len(listurl))+"页")#便于调试
return listurl
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
#若为URLerror异常,延迟10s进行
time.sleep(10)
except Exception as e:
print("exception:"+str(e))
#若为Exception异常,延迟1s进行
time.sleep(1) #函数:通过链接获取对应的内容
def getcontent(listurl,proxy):
i = 0
#设置本地文件中的开始html编码
html1 = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm11/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>微信文章页面</title>
</head>
<body>'''
with open("D:/WEB/1.html","wb")as f:
f.write(html1.encode("utf-8"))
#再次以追加写入的方式打开文件,以写入对应文章内容
with open("D:/WEB/1.html","ab")as ff:
#此时listurl为二维列表,形如listurl[][],第一维存储信息跟第几页相关,第二维存的跟该页第几个文章链接相关
for i in range(0,len(listurl)):
for j in range(0,len(listurl[i])):
try:
url = listurl[i][j]
#处理真实url,亦可观察对应网址的关系自行分析,采集网址比真实网址多了一串amp
url = url.replace("amp;","")
#使用代理去爬取对应网址的内容
data = use_proxy(proxy,url)
#文章标题正则表达式
titlepat = "<title>(.*?)</title>"
#文章内容正则表达式
contentpat = 'id="js_content">(.*?)id="js_sg_bar"'
#找到标题并赋给列表title
title = re.compile(titlepat).findall(data)
#找到内容并赋给列表content
content = re.compile(contentpat,re.S).findall(data)
#初始化标题与内容
thistitle = "此次没有获取到"
thiscontent= "此次没有获取到"
#如果标题列表不为空,说明找到了标题,取列表第0个元素,即此次标题赋给变量thistitle
if (title!=[]):
thistitle = title[0]
if (content!=[]):
thiscontent = content[0]
#将标题与内容汇总赋给变量dataall
dataall = '<p>标题为:'+thistitle+'</p><p>内容为:'+thiscontent+'</p><br>'
#将文章标题与内容写入对应文件
ff.write(dataall.encode("utf-8"))
print("第"+str(i)+"个网页第"+str(j)+"次处理")#便于调试
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
time.sleep(10)
except Exception as e:
print("exception:"+str(e))
time.sleep(1)
html2 = '''</body>
</html>
'''
with open("D:/WEB/1.html","ab")as f:
f.write(html2.encode("utf-8")) #设置关键词
key = "物联网"
#设置代理服务器
proxy = "122.72.32.73:80"
#可以为getlisturl()与getcontent设置不同的代理服务器,此处没有启用该项设置
proxy2 = ""
#启示页
pagesatrt = 1
#爬取到哪页
pageend = 2
listurl = getlisturl(key,pagesatrt,pageend,proxy)
getcontent(listurl,proxy)

python 抓取搜狗微信出现的问题,求大神解决的更多相关文章

  1. 第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    第三百三十节,web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解 封装模块 #!/usr/bin/env python # -*- coding: utf- ...

  2. 九 web爬虫讲解2—urllib库爬虫—实战爬取搜狗微信公众号—抓包软件安装Fiddler4讲解

    封装模块 #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib from urllib import request import j ...

  3. requests利用selenium,代理Ip,云打码,验证码抠图操作 爬取搜狗微信公众号内容

    爬取思路,爬取搜狗微信公众号内容,爬取第一层url时请求太快出现验证码,我这里用的蘑菇云代理,并在程序中我判断什么情况下是否+代理,做到合理运用代理ip.爬取第二层url时验证码出现次数更严重(和第一 ...

  4. Python 抓取网页并提取信息(程序详解)

    最近因项目需要用到python处理网页,因此学习相关知识.下面程序使用python抓取网页并提取信息,具体内容如下: #---------------------------------------- ...

  5. 使用 Python 抓取欧洲足球联赛数据

    Web Scraping在大数据时代,一切都要用数据来说话,大数据处理的过程一般需要经过以下的几个步骤    数据的采集和获取    数据的清洗,抽取,变形和装载    数据的分析,探索和预测    ...

  6. python抓取性感尤物美女图

    由于是只用标准库,装了python3运行本代码就能下载到多多的美女图... 写出代码前面部分的时候,我意识到自己的函数设计错了,强忍继续把代码写完. 测试发现速度一般,200K左右的下载速度,也没有很 ...

  7. python抓取网页例子

    python抓取网页例子 最近在学习python,刚刚完成了一个网页抓取的例子,通过python抓取全世界所有的学校以及学院的数据,并存为xml文件.数据源是人人网. 因为刚学习python,写的代码 ...

  8. Python抓取页面中超链接(URL)的三中方法比较(HTMLParser、pyquery、正则表达式) <转>

    Python抓取页面中超链接(URL)的3中方法比较(HTMLParser.pyquery.正则表达式) HTMLParser版: #!/usr/bin/python # -*- coding: UT ...

  9. 如何用python抓取js生成的数据 - SegmentFault

    如何用python抓取js生成的数据 - SegmentFault 如何用python抓取js生成的数据 1赞 踩 收藏 想写一个爬虫,但是需要抓去的的数据是js生成的,在源代码里看不到,要怎么才能抓 ...

随机推荐

  1. Kafka学习之(六)搭建kafka集群

    想要搭建kafka集群,必须具备zookeeper集群,关于zookeeper集群的搭建,在Kafka学习之(五)搭建kafka集群之Zookeeper集群搭建博客有说明.需要具备两台以上装有zook ...

  2. 开发代码code中变量替换

    除了automake/autoconfig 之外,还有其他的替换方式. 参看vdsm https://github.com/oVirt/vdsm/blob/master/Makefile.am htt ...

  3. msf辅助模块的应用

    msf辅助模块的应用 创建一个msf所需的数据库 进入模块 设置相关参数 查看 开启扫描

  4. 20165211 获奖感想及java课程总结

    20165211 获奖感想及java课程总结 理论脱离实践是最大的不幸.--达芬奇 这句话,是我在学习Java之前,假期内写的20165211 学习基础和C语言调查里的所引用的一句话,是当时我对Jav ...

  5. ["1", "2", "3"].map(parseInt) 为何返回[1,NaN,NaN]

    转载自:http://blog.csdn.net/freshlover/article/details/19034079 这涉及到是否深入理解两个函数的格式与参数含义. 首先根据我对两个函数用法的了解 ...

  6. C++使用TinyXML

    参照一:http://qaohao.iteye.com/blog/496237 参照二:http://hi.baidu.com/lnylvoeegzcgnrr/item/af68fd9cde40fc1 ...

  7. startActivityForResult( )用法

    一.与startActivity( )的不同之处 1, startActivity( ) 仅仅是跳转到目标页面,若是想跳回当前页面,则必须再使用一次startActivity( ). 2, start ...

  8. junit中test注解测试使用案列解析二

    本文原创,转载请注明出处 在上文中,已经简单的解析了junit中test注解的使用方法,今天在进行test测试时,遇到了一个异常,于是想深 入的研究一下. 还原一下今天的异常代码: @Service ...

  9. perl usage()和getopt

    #!/usr/bin/perl#perl search.pl --infile Targets.Lung.Carcinoma.genelist.txt --homogene Homo_sapiens. ...

  10. 通过gevent实现【单线程】下的多socket并发

    server import sys import socket import time import gevent from gevent import socket,monkey monkey.pa ...