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常用模块用法分析
Sep 08 Python
深入浅析python继承问题
May 29 Python
Python模拟登陆实现代码
Jun 14 Python
Django中的Signal代码详解
Feb 05 Python
使用python采集脚本之家电子书资源并自动下载到本地的实例脚本
Oct 23 Python
python处理excel绘制雷达图
Oct 18 Python
Python 实现向word(docx)中输出
Feb 13 Python
PyTorch: Softmax多分类实战操作
Jul 07 Python
Python实现快速大文件比较代码解析
Sep 04 Python
Python中Qslider控件实操详解
Feb 20 Python
k-means & DBSCAN 总结
Apr 27 Python
pandas中pd.groupby()的用法详解
Jun 16 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
迅雷下载《中学科技》怀旧期刊下载
2021/02/27 无线电
PHP开发入门教程之面向对象
2006/12/05 PHP
PHP数组传递是值传递而非引用传递概念纠正
2013/01/31 PHP
Linux下创建nginx脚本-start、stop、reload…
2014/08/03 PHP
php使用date和strtotime函数输出指定日期的方法
2014/11/14 PHP
使用jquery给input和textarea设定ie中的focus
2008/05/29 Javascript
jQuery表格排序组件-tablesorter使用示例
2014/05/26 Javascript
ext中store.load跟store.reload的区别示例介绍
2014/06/17 Javascript
随鼠标移动的时钟非常漂亮遗憾的是只支持IE
2014/08/12 Javascript
angularJS 中$scope方法使用指南
2015/02/09 Javascript
js实现从中间开始往上下展开网页窗口的方法
2015/03/02 Javascript
JS特效实现图片自动播放并可控的效果
2015/07/31 Javascript
详解AngularJS 模态对话框
2016/04/07 Javascript
Bootstrap CSS布局之按钮
2016/12/17 Javascript
vue数据双向绑定原理解析(get & set)
2017/03/08 Javascript
Vue 实现展开折叠效果的示例代码
2018/08/27 Javascript
Vue利用History记录上一页面的数据方法实例
2018/11/02 Javascript
解决微信小程序调用moveToLocation失效问题【超简单】
2019/04/12 Javascript
Vue使用zTree插件封装树组件操作示例
2019/04/25 Javascript
arctext.js实现文字平滑弯曲弧形效果的插件
2019/05/13 Javascript
Vue3 中的数据侦测的实现
2019/10/09 Javascript
Python牛刀小试密码爆破
2011/02/03 Python
Python实现的一个自动售饮料程序代码分享
2014/08/25 Python
浅谈python中的数字类型与处理工具
2017/08/02 Python
python利用rsa库做公钥解密的方法教程
2017/12/10 Python
20行python代码实现人脸识别
2019/05/05 Python
Python流行ORM框架sqlalchemy安装与使用教程
2019/06/04 Python
python爬虫的一个常见简单js反爬详解
2019/07/09 Python
Python改变对象的字符串显示的方法
2020/08/01 Python
css3设置box-pack和box-align让div里面的元素垂直居中
2014/09/01 HTML / CSS
纯css3实现效果超级炫的checkbox复选框和radio单选框
2014/09/01 HTML / CSS
使用CSS禁止textarea调整大小功能的方法
2015/03/13 HTML / CSS
HTML5 script元素async、defer异步加载使用介绍
2013/08/23 HTML / CSS
2014年安全工作总结范文
2014/11/13 职场文书
捐书活动倡议书
2015/04/27 职场文书
Python编写车票订购系统 Python实现快递收费系统
2022/08/14 Python