前提:

python3.4

windows

作用:通过搜狗的微信搜索接口http://weixin.sogou.com/来搜索相关微信文章,并将标题及相关链接导入Excel表格中

说明:需xlsxwriter模块,另程序编写时间为2017/7/11,以免之后程序无法使用可能是网站做过相关改变,程序较为简单,除去注释40多行。

正题:

思路:打开初始Url  --> 正则获取标题及链接  -->  改变page循环第二步  -->  将得到的标题及链接导入Excel

爬虫的第一步都是先手工操作一遍(闲话)

进入上面提到的网址,如输入:“图片识别”,搜索,网址变为“http://weixin.sogou.com/weixin?type=2&query=%E5%9B%BE%E7%89%87%E8%AF%86%E5%88%AB&ie=utf8&s_from=input&_sug_=n&_sug_type_=1&w=01015002&oq=&ri=4&sourceid=sugg&sut=0&sst0=1499778531195&lkt=0%2C0%2C0&p=40040108”标红为重要参数,type=1时是搜索公众号,暂且不管,query=‘搜索关键词’,关键词已经被编码,还有一个隐藏参数page=1

当你跳到第二页时可以看到“http://weixin.sogou.com/weixin?oq=&query=%E5%9B%BE%E7%89%87%E8%AF%86%E5%88%AB&_sug_type_=1&sut=0&lkt=0%2C0%2C0&s_from=input&ri=4&_sug_=n&type=2&sst0=1499778531195&page=2&ie=utf8&p=40040108&dp=1&w=01015002&dr=1”

好了,url可以得到了

1 url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)

search是要搜索的关键词,用quote()编码即可插入

1 search = urllib.request.quote(search)

page是用来循环的

1 for page in range(1,pagenum+1):
2 url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)

完整的url已经得到了,接下来访问url,获得其中的数据(创建opener对象,添加header())

1 import urllib.request
2 header = ('User-Agent','Mozilla/5.0')
3 opener = urllib.request.build_opener()
4 opener.addheaders = [header]
5 urllib.request.install_opener(opener)
6 data = urllib.request.urlopen(url).read().decode()

得到页面内容,采用正则表达获取相关数据

1 import re
2 finddata = re.compile('<a target="_blank" href="(.*?)".*?uigs="article_title_.*?">(.*?)</a>').findall(data)
3 #finddata = [('',''),('','')]

通过正则获取的数据中存在干扰项(链接:‘amp;’)和无关项(标题:'<em><...><....></em>'),用replace()解决

1 title = title.replace('<em><!--red_beg-->','')
2 title = title.replace('<!--red_end--></em>','')
1 link = link.replace('amp;','')

将处理后的标题和链接保存在列表中

1 title_link.append(link)
2 title_link.append(title)

如此搜索的标题和链接都得到了,接下来导入Excel

先创建Excel

1 import xlsxwriter
2 workbook = xlsxwriter.Workbook(search+'.xlsx')
3 worksheet = workbook.add_worksheet('微信')

将title_link中的数据导入Excel

1 for i in range(0,len(title_link),2):
2 worksheet.write('A'+str(i+1),title_link[i+1])
3 worksheet.write('C'+str(i+1),title_link[i])
4 workbook.close()

完整代码:

 1 '''
2 python3.4 + windows
3 羽凡-2017/7/11-
4 用于搜索微信文章,保存标题及链接至Excel中
5 每个页面10秒延迟,防止被限制
6 import urllib.request,xlsxwriter,re,time
7 '''
8 import urllib.request
9 search = str(input("搜索微信文章:"))
10 pagenum = int(input('搜索页数:'))
11 import xlsxwriter
12 workbook = xlsxwriter.Workbook(search+'.xlsx')
13 search = urllib.request.quote(search)
14 title_link = []
15 for page in range(1,pagenum+1):
16 url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)
17 import urllib.request
18 header = ('User-Agent','Mozilla/5.0')
19 opener = urllib.request.build_opener()
20 opener.addheaders = [header]
21 urllib.request.install_opener(opener)
22 data = urllib.request.urlopen(url).read().decode()
23 import re
24 finddata = re.compile('<a target="_blank" href="(.*?)".*?uigs="article_title_.*?">(.*?)</a>').findall(data)
25 #finddata = [('',''),('','')]
26 for i in range(len(finddata)):
27 title = finddata[i][1]
28 title = title.replace('<em><!--red_beg-->','')
29 title = title.replace('<!--red_end--></em>','')
30 try:
31 #标题中可能存在引号
32 title = title.replace('&ldquo;','"')
33 title = title.replace('&rdquo;','"')
34 except:
35 pass
36 link = finddata[i][0]
37 link = link.replace('amp;','')
38 title_link.append(link)
39 title_link.append(title)
40 print('第'+str(page)+'页')
41 import time
42 time.sleep(10)
43 worksheet = workbook.add_worksheet('微信')
44 worksheet.set_column('A:A',70)
45 worksheet.set_column('C:C',100)
46 bold = workbook.add_format({'bold':True})
47 worksheet.write('A1','标题',bold)
48 worksheet.write('C1','链接',bold)
49 for i in range(0,len(title_link),2):
50 worksheet.write('A'+str(i+1),title_link[i+1])
51 worksheet.write('C'+str(i+1),title_link[i])
52 workbook.close()
53 print('导入Excel完毕!')

python3之微信文章爬虫的更多相关文章

  1. [Python爬虫] 之十五:Selenium +phantomjs根据微信公众号抓取微信文章

    借助搜索微信搜索引擎进行抓取 抓取过程 1.首先在搜狗的微信搜索页面测试一下,这样能够让我们的思路更加清晰 在搜索引擎上使用微信公众号英文名进行“搜公众号”操作(因为公众号英文名是公众号唯一的,而中文 ...

  2. python爬虫实战(三)--------搜狗微信文章(IP代理池和用户代理池设定----scrapy)

    在学习scrapy爬虫框架中,肯定会涉及到IP代理池和User-Agent池的设定,规避网站的反爬. 这两天在看一个关于搜狗微信文章爬取的视频,里面有讲到ip代理池和用户代理池,在此结合自身的所了解的 ...

  3. 使用redis所维护的代理池抓取微信文章

    搜狗搜索可以直接搜索微信文章,本次就是利用搜狗搜搜出微信文章,获得详细的文章url来得到文章的信息.并把我们感兴趣的内容存入到mongodb中. 因为搜狗搜索微信文章的反爬虫比较强,经常封IP,所以要 ...

  4. web开发微信文章目录

    Web开发微信文章目录 2015-12-13 Web开发 本文是Web开发微信的文章目录.通过目录查看文章编号,回复文章编号就能查看文章全文. 回复编号查看全文,搜索分类名可以获得该分类下的文章.   ...

  5. php+mysql的微信文章发布平台

    如何在微信上发表丰富图文的文章? 最近在新浪云平台上做了一个php+mysql的微信文章发布平台,丫丫说. 在线编辑文章,扫一扫即可分享到微信,发到朋友圈,非常简单! http://yayashuo. ...

  6. selenium模拟浏览器对搜狗微信文章进行爬取

    在上一篇博客中使用redis所维护的代理池抓取微信文章,开始运行良好,之后运行时总是会报501错误,我用浏览器打开网页又能正常打开,调试了好多次都还是会出错,既然这种方法出错,那就用selenium模 ...

  7. php爬取微信文章内容

    php爬取微信文章内容 在做官网升级的时遇到新的需求,需要将公司公众号文章显示在官网的文章模块下.但存在的问题是:微信文章的链接会失效,并且需要对文章部分内容做修改,同时要减少微信运营人员的工作量,避 ...

  8. php解决微信文章图片防盗链

    解决微信文章图片防盗链 function actionWechatImg() { header('Content-type: image/jpg'); $url = $_GET['url']; $re ...

  9. Python3 itchat微信获取好友、公众号、群聊的基础信息

    Python3 itchat微信获取好友.公众号.群聊的基础信息 一.简介 安装 itchat pip install itchat 使用个人微信的过程当中主要有三种账号需要获取,分别为: 好友 公众 ...

随机推荐

  1. [0] WCF开发下,提示HTTP 无法注册 URL 进程不具有此命名空间的访问权限

    Visual Studio以管理员的身份运行就可以了.

  2. thinkphp 单字母函数

    在ThinkPHP中有许多使用简便的单字母函数(即快捷方法),可以很方便开发者快速的调用,但是字母函数却不方便记忆,本文将所有的字母函数总结一下,以方便以后查找. 1.U() URL组装 支持不同UR ...

  3. selenium webDriver给隐藏域赋值 input hidden set value

    //直接这样无法给input hidden赋值// driver.findElement(By.id("image_default")).sendKeys("a1112. ...

  4. 数据的ID名生成新的引用索引树

    <?php $arr= [ '0'=>[ "id"=>2, "name"=>"建材", "pid" ...

  5. 怎么样刷新frameset的整个页面

    <a href="main.html?a=2" target="_parent">?  设置a链接的target属性值为_parent即可

  6. 页面刷新vuex数据消失问题解决方案

    VBox持续进行中,哀家苦啊,有没有谁给个star. vuex是vue用于数据存储的,和redux充当同样的角色. 最近在VBox开发的时候遇到的问题,页面刷新或者关闭浏览器再次打开的时候数据归零.这 ...

  7. 4.vbs的循环,switch,判断等语句

    1.条件判断语句 If Then Else Sub judge(x) Then MsgBox "the num is 0" Then MsgBox "the num is ...

  8. 2017寒假零基础学习Python系列之函数之 返回多个值

    Python也和C语言一样有自己的标准库,不过在Python中叫做模块(module),这个和C语言中的头文件以及Java中的包类似,其中math就是其中之一,math模块中提供了sin()和cos( ...

  9. 简单来说一下ui-route

    UI-Router被认为是AngularUI为开发者提供的最实用的一个模块,它是一个让开发者能够根据URL状态或者说是'机器状态'来组织和控制界面UI的渲染,而不是仅仅只改变路由(传统AngularJ ...

  10. (转载)IQ 16.0 SP02起支持从压缩文件直接装载数据到表中

    参考文档: http://m.blog.chinaunix.net/uid-16765068-id-4405877.htmlhttp://www.cnblogs.com/lichmama/p/4103 ...