python抓取搜狗微信公众号文章


Posted in Python onApril 01, 2019

初学python,抓取搜狗微信公众号文章存入mysql

mysql表:

python抓取搜狗微信公众号文章

python抓取搜狗微信公众号文章

代码:

import requests
import json
import re
import pymysql
 
# 创建连接
conn = pymysql.connect(host='你的数据库地址', port=端口, user='用户名', passwd='密码', db='数据库名称', charset='utf8')
# 创建游标
cursor = conn.cursor()

cursor.execute("select * from hd_gzh")
effect_row = cursor.fetchall()
from bs4 import BeautifulSoup

socket.setdefaulttimeout(60)
count = 1
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0'}
#阿布云ip代理暂时不用
# proxyHost = "http-cla.abuyun.com"
# proxyPort = "9030"
# # 代理隧道验证信息
# proxyUser = "H56761606429T7UC"
# proxyPass = "9168EB00C4167176"

# proxyMeta = "http://%(user)s:%(pass)s@%(host)s:%(port)s" % {
#  "host" : proxyHost,
#  "port" : proxyPort,
#  "user" : proxyUser,
#  "pass" : proxyPass,
# }

# proxies = {
#   "http" : proxyMeta,
#   "https" : proxyMeta,
# }

#查看是否已存在数据
def checkData(name):
  sql = "select * from gzh_article where title = '%s'"
  data = (name,)
  count = cursor.execute(sql % data)
  conn.commit()
  if(count!=0):
    return False
  else:
    return True
#插入数据
def insertData(title,picture,author,content):
  sql = "insert into gzh_article (title,picture,author,content) values ('%s', '%s','%s', '%s')"
  data = (title,picture,author,content)
  cursor.execute(sql % data)
  conn.commit()
  print("插入一条数据")
  return
  
for row in effect_row:
  newsurl = 'https://weixin.sogou.com/weixin?type=1&s_from=input&query=' + row[1] + '&ie=utf8&_sug_=n&_sug_type_='
  res = requests.get(newsurl,headers=headers)
  res.encoding = 'utf-8'
  soup = BeautifulSoup(res.text,'html.parser')
  url = 'https://weixin.sogou.com' + soup.select('.tit a')[0]['href']
  res2 = requests.get(url,headers=headers)
  res2.encoding = 'utf-8'
  soup2 = BeautifulSoup(res2.text,'html.parser')
  pattern = re.compile(r"url \+= '(.*?)';", re.MULTILINE | re.DOTALL)
  script = soup2.find("script")
  url2 = pattern.search(script.text).group(1)
  res3 = requests.get(url2,headers=headers)
  res3.encoding = 'utf-8'
  soup3 = BeautifulSoup(res3.text,'html.parser')
  print()
  pattern2 = re.compile(r"var msgList = (.*?);$", re.MULTILINE | re.DOTALL)
  script2 = soup3.find("script", text=pattern2)
  s2 = json.loads(pattern2.search(script2.text).group(1))
  #等待10s
  time.sleep(10)
  
  for news in s2["list"]:
    articleurl = "https://mp.weixin.qq.com"+news["app_msg_ext_info"]["content_url"]
    articleurl = articleurl.replace('&','&')
    res4 = requests.get(articleurl,headers=headers)
    res4.encoding = 'utf-8'
    soup4 = BeautifulSoup(res4.text,'html.parser')
    if(checkData(news["app_msg_ext_info"]["title"])):
      insertData(news["app_msg_ext_info"]["title"],news["app_msg_ext_info"]["cover"],news["app_msg_ext_info"]["author"],pymysql.escape_string(str(soup4)))
    count += 1
    #等待5s
    time.sleep(10)
    for news2 in news["app_msg_ext_info"]["multi_app_msg_item_list"]:
      articleurl2 = "https://mp.weixin.qq.com"+news2["content_url"]
      articleurl2 = articleurl2.replace('&','&')
      res5 = requests.get(articleurl2,headers=headers)
      res5.encoding = 'utf-8'
      soup5 = BeautifulSoup(res5.text,'html.parser')
      if(checkData(news2["title"])):
        insertData(news2["title"],news2["cover"],news2["author"],pymysql.escape_string(str(soup5)))
      count += 1
      #等待10s
      time.sleep(10)
cursor.close()
conn.close()
print("操作完成")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python动态监控日志内容的示例
Feb 16 Python
使用PDB简单调试Python程序简明指南
Apr 25 Python
深入探究Python中变量的拷贝和作用域问题
May 05 Python
用Python实现一个简单的多线程TCP服务器的教程
May 05 Python
数组保存为txt, npy, csv 文件, 数组遍历enumerate的方法
Jul 09 Python
啥是佩奇?使用Python自动绘画小猪佩奇的代码实例
Feb 20 Python
深入了解Python枚举类型的相关知识
Jul 09 Python
Python拆分大型CSV文件代码实例
Oct 07 Python
Python使用socket模块实现简单tcp通信
Aug 18 Python
python中reload重载实例用法
Dec 15 Python
matplotlib绘制鼠标的十字光标的实现(内置方式)
Jan 06 Python
对Pytorch 中的contiguous理解说明
Mar 03 Python
Python使用os.listdir()和os.walk()获取文件路径与文件下所有目录的方法
Apr 01 #Python
python装饰器简介---这一篇也许就够了(推荐)
Apr 01 #Python
Python批量删除只保留最近几天table的代码实例
Apr 01 #Python
Python中的Socket 与 ScoketServer 通信及遇到问题解决方法
Apr 01 #Python
python assert的用处示例详解
Apr 01 #Python
使用Python操作FTP实现上传和下载的方法
Apr 01 #Python
Python提取特定时间段内数据的方法实例
Apr 01 #Python
You might like
PHP时间戳与日期之间转换的实例介绍
2013/04/19 PHP
CodeIgniter框架提示Disallowed Key Characters的解决办法
2014/04/21 PHP
windows7下安装php的imagick和imagemagick扩展教程
2014/07/04 PHP
php类的自动加载操作实例详解
2016/09/28 PHP
php语法检查的方法总结
2019/01/21 PHP
laravel 解决后端无法获取到前端Post过来的值问题
2019/10/22 PHP
js中访问html中iframe的文档对象的代码[IE6,IE7,IE8,FF]
2011/01/08 Javascript
解析John Resig Simple JavaScript Inheritance代码
2012/12/03 Javascript
Ajax清除浏览器js、css、图片缓存的方法
2015/08/06 Javascript
JS通过Cookie判断页面是否为首次打开
2016/02/05 Javascript
JS对大量数据进行多重过滤的方法
2016/11/04 Javascript
JavaScript中附件预览功能实现详解(推荐)
2017/08/15 Javascript
vue获取input输入值的问题解决办法
2017/10/17 Javascript
vue中的模态对话框组件实现过程
2018/05/01 Javascript
微信小程序页面间传递数组对象方法解析
2019/11/06 Javascript
[35:55]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.11
2020/12/13 DOTA
Python collections模块实例讲解
2014/04/07 Python
在Python中使用M2Crypto模块实现AES加密的教程
2015/04/08 Python
Python生成不重复随机值的方法
2015/05/11 Python
浅谈Python 集合(set)类型的操作——并交差
2016/06/30 Python
python获取指定时间差的时间实例详解
2017/04/11 Python
CentOS 7下安装Python 3.5并与Python2.7兼容并存详解
2017/07/07 Python
numpy中索引和切片详解
2017/12/15 Python
对Python中数组的几种使用方法总结
2018/06/28 Python
Python 微信爬虫完整实例【单线程与多线程】
2019/07/06 Python
python 寻找离散序列极值点的方法
2019/07/10 Python
TensorFlow tensor的拼接实例
2020/01/19 Python
Python实现自动打开电脑应用的示例代码
2020/04/17 Python
Pytorch转keras的有效方法,以FlowNet为例讲解
2020/05/26 Python
用Python 执行cmd命令
2020/12/18 Python
Woolworth官网:澳洲第一大超市
2017/06/25 全球购物
联想新西兰官方网站:Lenovo New Zealand
2018/10/30 全球购物
本科应届生自荐信
2014/06/29 职场文书
股指期货心得体会
2014/09/10 职场文书
2014年加油站工作总结
2014/12/04 职场文书
世界名著读书笔记
2015/06/25 职场文书