python抓取搜狗微信公众号文章

 更新时间:2019年04月01日 14:17:55   作者:萌力突破  
这篇文章主要为大家详细介绍了python抓取搜狗微信公众号文章,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

初学python,抓取搜狗微信公众号文章存入mysql

mysql表:

代码:

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迭代器和生成器定义与用法示例

    这篇文章主要介绍了Python迭代器和生成器定义与用法,结合实例形式详细分析了Python迭代器和生成器的概念、原理、定义、使用方法及相关操作注意事项,需要的朋友可以参考下
    2018-02-02
  • python中的对数log函数表示及用法

    python中的对数log函数表示及用法

    在本篇文章里小编给大家整理了一篇关于python中的对数log函数表示及用法,有需要的朋友们可以学习下。
    2020-12-12
  • python3简单实现微信爬虫

    python3简单实现微信爬虫

    我们可以通过python 来实现这样一个简单的爬虫功能,把我们想要的代码爬取到本地。下面就看看如何使用python来实现这样一个功能。
    2015-04-04
  • python模拟登录百度贴吧(百度贴吧登录)实例

    python模拟登录百度贴吧(百度贴吧登录)实例

    python模拟登录百度贴吧实例分享,大家参考使用吧
    2013-12-12
  • python+numpy实现的基本矩阵操作示例

    python+numpy实现的基本矩阵操作示例

    这篇文章主要介绍了python+numpy实现的基本矩阵操作,结合实例形式分析了Python使用numpy模块针对矩阵进行创建、增删查改、索引、运算相关操作实现技巧,注释中包含有详细的说明,需要的朋友可以参考下
    2019-07-07
  • python定时复制远程文件夹中所有文件

    python定时复制远程文件夹中所有文件

    这篇文章主要为大家详细介绍了python定时复制远程文件夹中所有文件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-04-04
  • 详解Python3中ceil()函数用法

    详解Python3中ceil()函数用法

    在本篇内容里我们给大家整理了关于Python3中ceil()函数用法以及相关知识点,需要的学习一下吧。
    2019-02-02
  • 探索Python random模块随机性神奇世界

    探索Python random模块随机性神奇世界

    Python中的random模块提供了丰富的工具和函数,帮助我们生成随机数、操作随机序列,以及模拟随机性事件,在本文中,我们将分享random模块,了解它的基本用法、功能和应用领域,并提供示例代码来帮助你更好地理解随机性的神奇世界
    2023-11-11
  • Python+wxPython实现自动生成PPTX文档程序

    Python+wxPython实现自动生成PPTX文档程序

    这篇文章主要介绍了如何使用 wxPython 模块和 python-pptx 模块来编写一个程序,用于生成包含首页、内容页和感谢页的 PPTX 文档,感兴趣的小伙伴可以学习一下
    2023-08-08
  • python解析xml模块封装代码

    python解析xml模块封装代码

    这篇文章主要分享下在python中解析xml文件的模块用法,以及对模块封装的方法,有需要的朋友参考下
    2014-02-02

最新评论