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发送Email方法实例
Aug 21 Python
python利用Guetzli批量压缩图片
Mar 23 Python
Python中矩阵库Numpy基本操作详解
Nov 21 Python
python实现两个文件合并功能
Apr 01 Python
Python中的取模运算方法
Nov 10 Python
Python3实现获取图片文字里中文的方法分析
Dec 13 Python
python ChainMap的使用和说明详解
Jun 11 Python
python自动发微信监控报警
Sep 06 Python
python中p-value的实现方式
Dec 16 Python
Django调用支付宝接口代码实例详解
Apr 04 Python
Python接收手机短信的代码整理
Aug 02 Python
python数字转对应中文的方法总结
Aug 02 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脚本的10个技巧(4)
2006/10/09 PHP
phpmyadmin中禁止外网使用的方法
2014/11/04 PHP
thinkPHP下的widget扩展用法实例分析
2015/12/26 PHP
PHP 中TP5 Request 请求对象的实例详解
2017/07/31 PHP
php 多继承的几种常见实现方法示例
2019/11/18 PHP
javascript温习的一些笔记 基础常用知识小结
2011/06/22 Javascript
JQuery入门——用one()方法绑定事件处理函数(仅触发一次)
2013/02/05 Javascript
在服务端(Page.Write)调用自定义的JS方法详解
2013/08/09 Javascript
Jquery uploadify图片上传插件无法上传的解决方法
2013/12/16 Javascript
js代码实现的加入收藏效果并兼容主流浏览器
2014/06/23 Javascript
JS组件中bootstrap multiselect两大组件较量
2016/01/26 Javascript
原生javascript实现解析XML文档与字符串
2016/03/01 Javascript
简单理解js的prototype属性及使用
2016/12/07 Javascript
老生常谈ES6中的类
2017/07/31 Javascript
javascript兼容性(实例讲解)
2017/08/15 Javascript
浅谈VUE-CLI脚手架热更新太慢的原因和解决方法
2018/09/28 Javascript
JS检索下拉列表框中被选项目的索引号(selectedIndex)
2019/12/17 Javascript
JavaScript原生数组函数实例汇总
2020/10/14 Javascript
python 将字符串转换成字典dict
2013/03/24 Python
Python实现同时兼容老版和新版Socket协议的一个简单WebSocket服务器
2014/06/04 Python
Python和GO语言实现的消息摘要算法示例
2015/03/10 Python
详细讲解用Python发送SMTP邮件的教程
2015/04/29 Python
Python记录详细调用堆栈日志的方法
2015/05/05 Python
详解Python发送邮件实例
2016/01/10 Python
python3 tkinter实现点击一个按钮跳出另一个窗口的方法
2019/06/13 Python
用python写爬虫简单吗
2020/07/28 Python
Conforama瑞士:家具、厨房、电器、装饰
2020/09/06 全球购物
eBay荷兰购物网站:eBay.nl
2020/06/26 全球购物
你所知道的集合类都有哪些?主要方法?
2012/12/31 面试题
大学毕业生自荐书怎么写?
2014/01/06 职场文书
2014年中班元旦活动方案
2014/02/14 职场文书
授权委托书范本
2014/04/03 职场文书
大学生个人求职信
2014/06/02 职场文书
借条如何写
2015/05/26 职场文书
两行代码解决Jupyter Notebook中文不能显示的问题
2021/04/24 Python
分享3个非常实用的 Python 模块
2022/03/03 Python