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获取Windows或Linux主机名称通用函数分享
Nov 22 Python
Python算法之求n个节点不同二叉树个数
Oct 27 Python
Python实现希尔排序算法的原理与用法实例分析
Nov 23 Python
Python多线程编程之多线程加锁操作示例
Sep 06 Python
Python  unittest单元测试框架的使用
Sep 08 Python
Tesserocr库的正确安装方式
Oct 19 Python
在Pycharm中执行scrapy命令的方法
Jan 16 Python
python求最大值最小值方法总结
Jun 25 Python
python实现在多维数组中挑选符合条件的全部元素
Nov 26 Python
小结Python的反射机制
Sep 28 Python
详解Python中@staticmethod和@classmethod区别及使用示例代码
Dec 14 Python
python神经网络 tf.name_scope 和 tf.variable_scope 的区别
May 04 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 判断变量类型实现代码
2009/10/23 PHP
解析PHP汉字转换拼音的类
2013/06/18 PHP
CMS中PHP判断系统是否已经安装的方法示例
2014/07/26 PHP
PHP判断网络文件是否存在的方法
2015/03/12 PHP
Javascript 学习笔记 错误处理
2009/07/30 Javascript
JQery 渐变图片导航效果代码 漂亮
2010/01/01 Javascript
Js,alert出现乱码问题的解决方法
2013/06/19 Javascript
javaScript对文字按照拼音排序实现代码
2013/12/27 Javascript
js表单中选择框值的获取及表单的序列化
2015/12/17 Javascript
快速移动鼠标触发问题及解决方法(ECharts外部调用保存为图片操作及工作流接线mouseenter和mouseleave)
2016/08/29 Javascript
使用bootstrap validator的remote验证代码经验分享(推荐)
2016/09/21 Javascript
JavaScript cookie详解及简单实例应用
2016/12/31 Javascript
Bootstrap缩略图与警告框学习使用
2017/02/08 Javascript
JavaScript生成图形验证码
2020/08/24 Javascript
vue.js异步上传文件前后端实现代码
2017/08/22 Javascript
React通过父组件传递类名给子组件的实现方法
2017/11/13 Javascript
Vue.js在数组中插入重复数据的实现代码
2017/11/17 Javascript
JS获取并处理php数组的方法实例分析
2018/09/04 Javascript
JS+CSS实现随机点名(实例代码)
2019/11/04 Javascript
Vue结合路由配置递归实现菜单栏功能
2020/06/16 Javascript
python实现求两个字符串的最长公共子串方法
2018/07/20 Python
Python 使用PIL中的resize进行缩放的实例讲解
2018/08/03 Python
python实现根据给定坐标点生成多边形mask的例子
2020/02/18 Python
Pytorch十九种损失函数的使用详解
2020/04/29 Python
Pytorch 解决自定义子Module .cuda() tensor失败的问题
2020/06/23 Python
大学生自我鉴定评语
2014/01/27 职场文书
高中军训感言400字
2014/02/24 职场文书
竞选大队干部演讲稿
2014/09/11 职场文书
个人培训总结
2015/03/05 职场文书
幼儿园教师求职信
2015/03/20 职场文书
我的长征观后感
2015/06/09 职场文书
水浒传读书笔记
2015/06/25 职场文书
离婚财产分割协议书
2015/08/11 职场文书
MySQL对数据表已有表进行分区表的实现
2021/11/01 MySQL
一文弄懂MySQL索引创建原则
2022/02/28 MySQL
CentOS7安装GlusterFS集群以及相关配置
2022/04/12 Servers