设为首页 收藏本站
查看: 1201|回复: 0

[经验分享] Python 爬虫爬取微信文章

[复制链接]

尚未签到

发表于 2018-8-10 08:53:21 | 显示全部楼层 |阅读模式
#!/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=[headers]
  
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[1]))+&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[j]
  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[0]
  if (content!=[]):
  thiscontent = content[0]
  #将标题与内容汇总赋给变量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、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-549459-1-1.html 上篇帖子: Python第三方库paramiko SSH连接 下篇帖子: python3实现单例模式
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表