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中的RSS处理
Apr 13 Python
python使用range函数计算一组数和的方法
May 07 Python
Python常见格式化字符串方法小结【百分号与format方法】
Sep 18 Python
修复 Django migration 时遇到的问题解决
Jun 14 Python
破解安装Pycharm的方法
Oct 19 Python
linux环境中没有网络怎么下载python
Jul 07 Python
python range实例用法分享
Feb 06 Python
使用python采集Excel表中某一格数据
May 14 Python
Python-opencv实现红绿两色识别操作
Jun 04 Python
使用python实现名片管理系统
Jun 18 Python
python中format函数如何使用
Jun 22 Python
python使用建议技巧分享(三)
Aug 18 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/12/08 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
php设计模式之策略模式实例分析【星际争霸游戏案例】
2020/03/26 PHP
jquery用offset()方法获得元素的xy坐标
2014/09/06 Javascript
jQuery实现设置、移除文本框默认值功能
2015/01/13 Javascript
JavaScript之Object类型介绍
2015/04/01 Javascript
javascript图片滑动效果实现
2021/01/28 Javascript
js实现正则匹配中文标点符号的方法
2015/12/23 Javascript
jQuery实现元素拖拽并cookie保存顺序的方法
2016/02/20 Javascript
从重置input file标签中看jQuery的 .val() 和 .attr(“value”) 区别
2016/06/12 Javascript
jQuery Ajax使用FormData上传文件和其他数据后端web.py获取
2017/06/11 jQuery
node使用Koa2搭建web项目的方法
2017/10/17 Javascript
vue实现循环切换动画
2018/10/17 Javascript
vue通过cookie获取用户登录信息的思路详解
2018/10/30 Javascript
通过Nodejs搭建网站简单实现注册登录流程
2019/06/14 NodeJs
Vue中qs插件的使用详解
2020/02/07 Javascript
js实现无限层级树形数据结构(创新算法)
2020/02/27 Javascript
Python Nose框架编写测试用例方法
2017/10/26 Python
tensorflow建立一个简单的神经网络的方法
2018/02/10 Python
Numpy array数据的增、删、改、查实例
2018/06/04 Python
Python变量访问权限控制详解
2019/06/29 Python
基于python实现自动化办公学习笔记(CSV、word、Excel、PPT)
2019/08/06 Python
Python Django框架模板渲染功能示例
2019/11/08 Python
Python Tkinter Entry和Text的添加与使用详解
2020/03/04 Python
Python同时处理多个异常的方法
2020/07/28 Python
Python3合并两个有序数组代码实例
2020/08/11 Python
使用python对excel表格处理的一些小功能
2021/01/25 Python
css3利用transform变形结合事件完成扇形导航
2020/10/26 HTML / CSS
商务专员岗位职责
2013/11/23 职场文书
勾股定理课后反思
2014/04/26 职场文书
企业挂职心得体会
2014/09/10 职场文书
2014年扶贫帮困工作总结
2014/12/09 职场文书
中秋晚会致辞
2015/07/31 职场文书
导游词之江苏同里古镇
2019/11/18 职场文书
CSS filter 有什么神奇用途
2021/05/25 HTML / CSS
Flutter集成高德地图并添加自定义Maker的实践
2022/04/07 Java/Android