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基于ID3思想的决策树
Jan 03 Python
Python框架Flask的基本数据库操作方法分析
Jul 13 Python
flask中过滤器的使用详解
Aug 01 Python
linux安装Python3.4.2的操作方法
Sep 28 Python
Python assert语句的简单使用示例
Jul 28 Python
python 和c++实现旋转矩阵到欧拉角的变换方式
Dec 04 Python
django框架两个使用模板实例
Dec 11 Python
python类中super() 的使用解析
Dec 19 Python
Python Selenium参数配置方法解析
Jan 19 Python
Python通过socketserver处理多个链接
Mar 18 Python
Django用户登录与注册系统的实现示例
Jun 03 Python
PyQt5多线程防卡死和多窗口用法的实现
Sep 15 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
页面乱码问题的根源及其分析
2013/08/09 PHP
PHP静态文件生成类实例
2014/11/29 PHP
php生成不重复随机数、数组的4种方法分享
2015/03/30 PHP
PHP 返回13位时间戳的实现代码
2016/05/13 PHP
PHP读取文件的常见几种方法
2016/11/03 PHP
Json_decode 解析json字符串为NULL的解决方法(必看)
2017/02/17 PHP
PHP goto语句用法实例
2019/08/06 PHP
javascritp实现input输入框相关限制用法
2007/06/29 Javascript
JavaScript中常见陷阱小结
2010/04/27 Javascript
js日历功能对象
2012/01/12 Javascript
动态改变div的z-index属性的简单实例
2013/08/08 Javascript
jQuery 属性选择器element[herf*='value']使用示例
2013/10/20 Javascript
解析JavaScript中点号“.”的多义性
2013/12/02 Javascript
js获取html页面节点方法(递归方式)
2013/12/13 Javascript
javascript等号运算符使用详解
2015/04/16 Javascript
JQuery实现简单的图片滑动切换特效
2015/11/22 Javascript
基于jquery实现三级下拉菜单
2016/05/10 Javascript
jQuery获取及设置表单input各种类型值的方法小结
2016/05/24 Javascript
KnockoutJS 3.X API 第四章之数据控制流foreach绑定
2016/10/10 Javascript
Sublime Text新建.vue模板并高亮(图文教程)
2017/10/26 Javascript
Angularjs渲染的 using 指令的星级评分系统示例
2017/11/09 Javascript
javascript实现电脑和手机版样式切换
2017/11/10 Javascript
微信小程序使用form表单获取输入框数据的实例代码
2018/05/17 Javascript
vue router+vuex实现首页登录验证判断逻辑
2018/05/17 Javascript
vue elementui tree 任意级别拖拽功能代码
2020/08/31 Javascript
[36:43]NB vs Optic 2018国际邀请赛小组赛BO1 B组加赛 8.19
2018/08/21 DOTA
详解Python3中字符串中的数字提取方法
2017/01/14 Python
Python通过调用mysql存储过程实现更新数据功能示例
2018/04/03 Python
python将字符串转变成dict格式的实现
2019/11/18 Python
Expedia印度尼西亚站:预订酒店、廉价航班和度假套餐
2018/01/31 全球购物
工商管理专业实习大学生自我鉴定
2013/09/19 职场文书
计算机专业个人求职信范例
2013/09/23 职场文书
音乐课外活动总结
2015/05/09 职场文书
运动会通讯稿300字
2015/07/20 职场文书
pytest进阶教程之fixture函数详解
2021/03/29 Python
Mysql数据库值的添加、修改、删除及清空操作实例
2021/06/20 MySQL