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中列表、字典、元组、集合数据结构整理
Nov 20 Python
Python判断文件和文件夹是否存在的方法
May 21 Python
详解Golang 与python中的字符串反转
Jul 21 Python
tensorflow 获取模型所有参数总和数量的方法
Jun 14 Python
使用python绘制二元函数图像的实例
Feb 12 Python
Python增强赋值和共享引用注意事项小结
May 28 Python
Ubuntu18.04下python版本完美切换的解决方法
Jun 14 Python
使用pandas读取文件的实现
Jul 31 Python
用openCV和Python 实现图片对比,并标识出不同点的方式
Dec 19 Python
python实现的Iou与Giou代码
Jan 18 Python
python 使用tkinter+you-get实现视频下载器
Nov 17 Python
Python编程super应用场景及示例解析
Oct 05 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
虫族 Zerg 历史背景
2020/03/14 星际争霸
php 静态页面中显示动态内容
2009/08/14 PHP
php版微信公众号自定义分享内容实现方法
2016/09/22 PHP
JQuery Easyui Tree的oncheck事件实现代码
2010/05/28 Javascript
JQquery的一些使用心得分享
2012/08/01 Javascript
window.requestAnimationFrame是什么意思,怎么用
2013/01/13 Javascript
JavaScrip调试技巧之断点调试
2015/10/22 Javascript
JavaScript隐式类型转换
2016/03/15 Javascript
jQuery中通过ajax调用webservice传递数组参数的问题实例详解
2016/05/20 Javascript
jquery判断input值不为空的方法
2016/06/05 Javascript
vue-cli的eslint相关用法
2017/09/29 Javascript
nuxt框架中路由鉴权之Koa和Session的用法
2018/05/09 Javascript
解析vue路由异步组件和懒加载案例
2018/06/08 Javascript
[02:41]DOTA2英雄基础教程 谜团
2013/12/10 DOTA
[01:50:49]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Aster BO3 第三场 1月24日
2021/03/11 DOTA
python基础教程之类class定义使用方法
2014/02/20 Python
Python基于回溯法子集树模板解决m着色问题示例
2017/09/07 Python
Python实现PS滤镜特效Marble Filter玻璃条纹扭曲效果示例
2018/01/29 Python
解决python 输出是省略号的问题
2018/04/19 Python
Python中最好用的命令行参数解析工具(argparse)
2019/08/23 Python
pycharm实现在子类中添加一个父类没有的属性
2020/03/12 Python
Python request操作步骤及代码实例
2020/04/13 Python
python开发前景如何
2020/06/11 Python
Python DataFrame使用drop_duplicates()函数去重(保留重复值,取重复值)
2020/07/20 Python
html5指南-2.如何操作document metadata
2013/01/07 HTML / CSS
巴西最大的家具及装饰用品店:Mobly
2017/10/11 全球购物
2014年公司庆元旦活动方案
2014/03/05 职场文书
新课培训心得体会
2014/09/03 职场文书
个人查摆剖析材料
2014/10/16 职场文书
党员贯彻十八大精神思想汇报范文
2014/10/25 职场文书
员工辞职信范文大全
2015/05/12 职场文书
外科护士长工作总结
2015/08/12 职场文书
勤俭节约主题班会
2015/08/13 职场文书
七年级作文之英语老师
2019/10/28 职场文书
一文搞懂Golang 时间和日期相关函数
2021/12/06 Golang
MySQL如何修改字段类型和字段长度
2022/06/10 MySQL