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中使用Tkinter模块创建GUI程序实例
Jan 14 Python
详细解析Python当中的数据类型和变量
Apr 25 Python
Python中的descriptor描述器简明使用指南
Jun 02 Python
使用Kivy将python程序打包为apk文件
Jul 29 Python
python清理子进程机制剖析
Nov 23 Python
Python使用matplotlib简单绘图示例
Feb 01 Python
Python图像处理之简单画板实现方法示例
Aug 30 Python
浅谈Python traceback的优雅处理
Aug 31 Python
Python Multiprocessing多进程 使用tqdm显示进度条的实现
Aug 13 Python
使用ITK-SNAP进行抠图操作并保存mask的实例
Jul 01 Python
在Python中如何使用yield
Jun 07 Python
python实现简单的聊天小程序
Jul 07 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程序中的常见漏洞进行攻击(下)
2006/10/09 PHP
PHP的autoload自动加载机制使用说明
2010/12/28 PHP
解析阿里云ubuntu12.04环境下配置Apache+PHP+PHPmyadmin+MYsql
2013/06/26 PHP
PHP利用str_replace防注入的方法
2013/11/10 PHP
基于PHP的简单采集数据入库程序【续篇】
2014/07/30 PHP
PHP迭代器和生成器用法实例分析
2019/09/28 PHP
javascript new 需不需要继续使用
2009/07/02 Javascript
Mootools 1.2教程(2) DOM选择器
2009/09/14 Javascript
js中的scroll和offset 使用比较的实例与分析
2013/09/29 Javascript
结合JQ1.9通过js正则判断各种浏览器版本的方法
2013/12/30 Javascript
使用 jQuery.ajax 上传带文件的表单遇到的问题
2016/10/31 Javascript
JS禁止浏览器右键查看元素或按F12审查元素自动关闭页面示例代码
2017/09/07 Javascript
vue使用Element组件时v-for循环里的表单项验证方法
2018/06/28 Javascript
详解使用jest对vue项目进行单元测试
2018/09/07 Javascript
JavaScript对象拷贝与赋值操作实例分析
2018/12/10 Javascript
VUE实现图片验证码功能
2020/11/18 Javascript
Python的Django框架下管理站点的基本方法
2015/07/17 Python
详解C++编程中一元运算符的重载
2016/01/19 Python
利用matplotlib+numpy绘制多种绘图的方法实例
2017/05/03 Python
教你利用Python玩转histogram直方图的五种方法
2018/07/30 Python
Python2.7实现多进程下开发多线程示例
2019/05/31 Python
PyQt5 加载图片和文本文件的实例
2019/06/14 Python
django框架使用views.py的函数对表进行增删改查内容操作详解【models.py中表的创建、views.py中函数的使用,基于对象的跨表查询】
2019/12/12 Python
美国最值得信赖的宠物药房:Allivet
2019/03/23 全球购物
普天C++笔试题
2016/03/20 面试题
交通事故私了协议书
2014/04/16 职场文书
毕业大学生自荐信
2014/06/17 职场文书
运动会广播稿150字(9篇)
2014/09/20 职场文书
校园游戏活动新闻稿
2014/10/15 职场文书
2014教师年度思想工作总结
2014/11/10 职场文书
人民检察院起诉书
2015/05/20 职场文书
初一军训感言
2015/08/01 职场文书
《大禹治水》教学反思
2016/02/22 职场文书
2016年感恩教师节活动总结
2016/04/01 职场文书
基于Python实现股票收益率分析
2022/04/02 Python
《模拟人生4》推出新补丁 “婚礼奇缘”DLC终于得到修复
2022/04/03 其他游戏