ABKYH 发表于 2018-8-10 08:53:21

Python 爬虫爬取微信文章

#!/usr/bin/env python  
# -*- coding: utf-8 -*-
  

  
import re
  
import urllib.request
  
import time
  
import urllib.error
  

  
##模拟浏览器安装headers
  
headers=("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36")
  
opener=urllib.request.build_opener()
  
opener.addheaders=
  
urllib.request.install_opener(opener)
  
##设置列表用于存储链接
  
listurl=[]
  

  
##定义代理服务器函数
  
#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')
  
#data=str(data)
  
#return data
  
#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)
  
##定义获取页面所有文章链接
  
def getlisturl(key,pagestart,pageend):
  try:
  page=pagestart
  keycode=urllib.request.quote(key)
  
#pagecode=urllib.request.quote("&page")
  for page in range(pagestart,pageend+1):
  url="http://weixin.sogou.com/weixin?type=2&query="+keycode+"&page="+str(page)
  data1=urllib.request.urlopen(url).read().decode('utf-8')
  data1=str(data1)
  listurlpat='<a data-z=&quot;art&quot;.*?(http://.*?)&quot;'
  listurl.append(re.compile(listurlpat,re.S).findall(data1))
  time.sleep(2)
  print(&quot;共获取到&quot;+str(len(listurl))+&quot;页&quot;)
  print(&quot;第2页链接数&quot;+str(len(listurl))+&quot;个&quot;)
  return listurl
  except urllib.error.URLError as e:
  if hasattr(e,&quot;code&quot;):
  print(e.code)
  if hasattr(e,&quot;reason&quot;):
  print(e.reason)
  time.sleep(10)
  except Exception as e:
  print(&quot;exception&quot;+str(e))
  time.sleep(1)
  

  
##定义获取文章内容
  
def getcontent(listurl):
  i = 0
  #设置本地文件中的开始html编码
  html1 = '''
  <!DOCTYPE html>
  <html>
  <head>
  <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; &quot; />
  <title>微信文章页面</title>
  </head>
  <body>
  '''
  fh=open(&quot;/home/urllib/test/1.html&quot;,&quot;wb&quot;)
  fh.write(html1.encode(&quot;utf-8&quot;))
  fh.close()
  #再次以追加写入的方式打开文件,以写入对应文章内容
  fh=open(&quot;/home/urllib/test/1.html&quot;,&quot;ab&quot;)
  for i in range(0,len(listurl)):
  for j in range(0,len(listurl)):
  try:
  url=listurl
  url=url.replace("amp;&quot;,&quot;&quot;)
  data=urllib.request.urlopen(url).read().decode('utf-8')
  data=str(data)
  titlepat='var msg_title = &quot;(.*?)&quot;;'
  contentpat='id=&quot;js_content&quot;>(.*?)id=&quot;js_sg_bar&quot;'
  title=re.compile(titlepat).findall(data)
  content=re.compile(contentpat,re.S).findall(data)
  #初始化标题与内容
  thistitle = &quot;此次没有获取到&quot;
  thiscontent= &quot;此次没有获取到&quot;
  #如果标题列表不为空,说明找到了标题,取列表第0个元素,即此次标题赋给变量thistitle
  if (title!=[]):
  thistitle = title
  if (content!=[]):
  thiscontent = content
  #将标题与内容汇总赋给变量dataall
  dataall = &quot;<p>标题为:&quot;+thistitle+&quot;</p><p>内容为:&quot;+thiscontent+&quot;</p><br>&quot;
  fh.write(dataall.encode('utf-8'))
  print(&quot;第&quot;+str(i)+&quot;个网页第&quot;+str(j)+&quot;次处理&quot;)
  time.sleep(1)
  except urllib.error.URLError as e:
  if hasattr(e,&quot;code&quot;):
  print(e.code)
  if hasattr(e,&quot;reason&quot;):
  print(e.reason)
  time.sleep(10)
  except Exception as e:
  print(&quot;exception&quot;+str(e))
  time.sleep(1)
  fh.close()
  html2='''</body>
  </html>
  '''
  fh=open(&quot;/home/urllib/test/1.html&quot;,&quot;ab&quot;)
  fh.write(html2.encode(&quot;utf-8&quot;))
  fh.close()
  
key=&quot;科技&quot;
  
#proxy=&quot;122.114.31.177:808&quot;
  
pagestart=1
  
pageend=3
  
listurl=getlisturl(key,pagestart,pageend)
  
getcontent(listurl)
页: [1]
查看完整版本: Python 爬虫爬取微信文章