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

yipeiwu_com6年前Python爬虫

前提:

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('“','"')
   title = title.replace('”','"')
  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设计】。

相关文章

urllib和BeautifulSoup爬取维基百科的词条简单实例

urllib和BeautifulSoup爬取维基百科的词条简单实例

本文实例主要实现的是使用urllib和BeautifulSoup爬取维基百科的词条,具体如下。 简洁代码: #引入开发包 from urllib.request import url...

requests和lxml实现爬虫的方法

如下所示: # requests模块来请求页面 # lxml模块的html构建selector选择器(格式化响应response) # from lxml import html # i...

python3简单实现微信爬虫

使用ghost.py 通过搜搜 的微信搜索来爬取微信公共账号的信息 # -*- coding: utf-8 -*- import sys reload(sys) import dat...

Python 制作糗事百科爬虫实例

Python 制作糗事百科爬虫实例

早上起来闲来无事做,莫名其妙的就弹出了糗事百科的段子,转念一想既然你送上门来,那我就写个爬虫到你网站上爬一爬吧,一来当做练练手,二来也算找点乐子。 其实这两天也正在接触数据库的内容,可以...

python爬虫实例详解

python爬虫实例详解

本篇博文主要讲解Python爬虫实例,重点包括爬虫技术架构,组成爬虫的关键模块:URL管理器、HTML下载器和HTML解析器。 爬虫简单架构 程序入口函数(爬虫调度段) #codi...