python3之微信文章爬虫实例讲解

所属分类: 脚本专栏 / python 阅读数: 1728
收藏 0 赞 0 分享

前提:

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可以得到了

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

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

search = urllib.request.quote(search)

page是用来循环的

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

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

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

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

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

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

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

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

 title_link.append(link)
 title_link.append(title)

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

先创建Excel

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

将title_link中的数据导入Excel

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

完整代码:

'''
python3.4 + windows
羽凡-2017/7/11-
用于搜索微信文章,保存标题及链接至Excel中
每个页面10秒延迟,防止被限制
import urllib.request,xlsxwriter,re,time
'''
import urllib.request
search = str(input("搜索微信文章:"))
pagenum = int(input('搜索页数:'))
import xlsxwriter
workbook = xlsxwriter.Workbook(search+'.xlsx')
search = urllib.request.quote(search)
title_link = []
for page in range(1,pagenum+1):
 url = 'http://weixin.sogou.com/weixin?type=2&query='+search+'&page='+str(page)
 import urllib.request
 header = ('User-Agent','Mozilla/5.0')
 opener = urllib.request.build_opener()
 opener.addheaders = [header]
 urllib.request.install_opener(opener)
 data = urllib.request.urlopen(url).read().decode()
 import re
 finddata = re.compile('<a target="_blank" href="(.*?)" rel="external nofollow" rel="external nofollow" .*?uigs="article_title_.*?">(.*?)</a>').findall(data)
 #finddata = [('',''),('','')]
 for i in range(len(finddata)):
  title = finddata[i][1]
  title = title.replace('<em><!--red_beg-->','')
  title = title.replace('<!--red_end--></em>','')
  try:
   #标题中可能存在引号
   title = title.replace('&ldquo;','"')
   title = title.replace('&rdquo;','"')
  except:
   pass
  link = finddata[i][0]
  link = link.replace('amp;','')
  title_link.append(link)
  title_link.append(title)
 print('第'+str(page)+'页')
 import time
 time.sleep(10)
worksheet = workbook.add_worksheet('微信')
worksheet.set_column('A:A',70)
worksheet.set_column('C:C',100)
bold = workbook.add_format({'bold':True})
worksheet.write('A1','标题',bold)
worksheet.write('C1','链接',bold)
for i in range(0,len(title_link),2):
 worksheet.write('A'+str(i+1),title_link[i+1])
 worksheet.write('C'+str(i+1),title_link[i])
workbook.close()
print('导入Excel完毕!')

以上这篇python3之微信文章爬虫实例讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Python实现按学生年龄排序的实际问题详解

这篇文章主要给大家介绍了关于Python实现按学生年龄排序实际问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Python开发的HTTP库requests详解

Requests是用Python语言编写,基于urllib,采用Apache2 Licensed开源协议的HTTP库。它比urllib更加方便,可以节约我们大量的工作,完全满足HTTP测试需求。Requests的哲学是以PEP 20 的习语为中心开发的,所以它比urllib更加P
收藏 0 赞 0 分享

Python网络爬虫与信息提取(实例讲解)

下面小编就为大家带来一篇Python网络爬虫与信息提取(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

在python3环境下的Django中使用MySQL数据库的实例

下面小编就为大家带来一篇在python3环境下的Django中使用MySQL数据库的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Python 3.x读写csv文件中数字的方法示例

在我们日常开发中经常需要对csv文件进行读写,下面这篇文章主要给大家介绍了关于Python 3.x读写csv文件中数字的相关资料,文中通过示例代码介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。
收藏 0 赞 0 分享

Python实现解析Bit Torrent种子文件内容的方法

这篇文章主要介绍了Python实现解析Bit Torrent种子文件内容的方法,结合实例形式分析了Python针对Torrent文件的读取与解析相关操作技巧与注意事项,需要的朋友可以参考下
收藏 0 赞 0 分享

Python实现文件内容批量追加的方法示例

这篇文章主要介绍了Python实现文件内容批量追加的方法,结合实例形式分析了Python文件的读写相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Python简单实现自动删除目录下空文件夹的方法

这篇文章主要介绍了Python简单实现自动删除目录下空文件夹的方法,涉及Python针对文件与目录的读取、判断、删除等相关操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

简单学习Python多进程Multiprocessing

这篇文章主要和大家一起简单的学习Python多进程Multiprocessing ,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Python导入模块时遇到的错误分析

这篇文章主要给大家详细解释了在Python处理导入模块的时候出现错误以及具体的情况分析,非常的详尽,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多