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中self原理实例分析
Apr 30 Python
详解常用查找数据结构及算法(Python实现)
Dec 09 Python
Python编程实现双链表,栈,队列及二叉树的方法示例
Nov 01 Python
pandas获取groupby分组里最大值所在的行方法
Apr 20 Python
Django项目开发中cookies和session的常用操作分析
Jul 03 Python
Python拼接字符串的7种方法总结
Nov 01 Python
python 求一个列表中所有元素的乘积实例
Jun 11 Python
使用 Python 快速实现 HTTP 和 FTP 服务器的方法
Jul 22 Python
Django中自定义查询对象的具体使用
Oct 13 Python
python利用dlib获取人脸的68个landmark
Nov 27 Python
python通过matplotlib生成复合饼图
Feb 06 Python
如何用tempfile库创建python进程中的临时文件
Jan 28 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
基于OpenCV的PHP图像人脸识别技术
2009/10/11 PHP
php有效防止图片盗用、盗链的两种方法
2016/11/01 PHP
PHP设计模式之装饰器模式实例详解
2018/02/07 PHP
关于JavaScript的gzip静态压缩方法
2007/01/05 Javascript
13个绚丽的Jquery 界面设计网站推荐
2010/09/28 Javascript
js setTimeout 常见问题小结
2013/08/13 Javascript
JavaScript实现16进制颜色值转RGB的方法
2015/02/09 Javascript
原生js实现数字字母混合验证码的简单实例
2015/12/10 Javascript
给angular加上动画效遇到的问题总结
2016/02/17 Javascript
Webpack 实现 AngularJS 的延迟加载
2016/03/02 Javascript
js实现一个猜数字游戏
2017/03/31 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
Angular获取手机验证码实现移动端登录注册功能
2017/05/17 Javascript
原生JS实现的双色球功能示例
2018/02/02 Javascript
如何在node环境实现“get数据解析”代码实例
2020/07/03 Javascript
[55:16]Mski vs VGJ.S Supermajor小组赛C组 BO3 第二场 6.3
2018/06/04 DOTA
[01:25:38]DOTA2-DPC中国联赛 正赛 VG vs LBZS BO3 第一场 1月19日
2021/03/11 DOTA
Python实现快速排序算法及去重的快速排序的简单示例
2016/06/26 Python
关于Python的一些学习总结
2018/05/25 Python
详解Python的三种可变参数
2019/05/08 Python
python中的colorlog库使用详解
2019/07/05 Python
python进阶之自定义可迭代的类
2019/08/20 Python
Python 生成器,迭代,yield关键字,send()传参给yield语句操作示例
2019/10/12 Python
基于keras 模型、结构、权重保存的实现
2020/01/24 Python
CSS3 特效范例整理
2011/08/22 HTML / CSS
Collection和Collections的区别
2016/05/02 面试题
法学专业应届生求职信
2013/10/16 职场文书
党员党性分析材料
2014/02/17 职场文书
会计专业求职信范文
2014/03/16 职场文书
《一个小村庄的故事》教学反思
2014/04/13 职场文书
效能监察建议书
2014/05/19 职场文书
个人查摆问题整改措施
2014/10/04 职场文书
中学生自我评价范文
2015/03/03 职场文书
2015年评职称工作总结范文
2015/04/20 职场文书
2015年化工厂工作总结
2015/05/04 职场文书
MySQL 使用事件(Events)完成计划任务
2021/05/24 MySQL