最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python抓取搜狗微信公眾號文章

 更新時間:2019年04月01日 14:17:55   作者:萌力突破  
這篇文章主要為大家詳細介紹了python抓取搜狗微信公眾號文章,具有一定的參考價值,感興趣的小伙伴們可以參考一下

初學python,抓取搜狗微信公眾號文章存入mysql

mysql表:

代碼:

import requests
import json
import re
import pymysql
 
# 創(chuàng)建連接
conn = pymysql.connect(host='你的數(shù)據(jù)庫地址', port=端口, user='用戶名', passwd='密碼', db='數(shù)據(jù)庫名稱', charset='utf8')
# 創(chuàng)建游標
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,
# }

#查看是否已存在數(shù)據(jù)
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
#插入數(shù)據(jù)
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("插入一條數(shù)據(jù)")
  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("操作完成")

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python迭代器和生成器定義與用法示例

    Python迭代器和生成器定義與用法示例

    這篇文章主要介紹了Python迭代器和生成器定義與用法,結(jié)合實例形式詳細分析了Python迭代器和生成器的概念、原理、定義、使用方法及相關操作注意事項,需要的朋友可以參考下
    2018-02-02
  • python中的對數(shù)log函數(shù)表示及用法

    python中的對數(shù)log函數(shù)表示及用法

    在本篇文章里小編給大家整理了一篇關于python中的對數(shù)log函數(shù)表示及用法,有需要的朋友們可以學習下。
    2020-12-12
  • python3簡單實現(xiàn)微信爬蟲

    python3簡單實現(xiàn)微信爬蟲

    我們可以通過python 來實現(xiàn)這樣一個簡單的爬蟲功能,把我們想要的代碼爬取到本地。下面就看看如何使用python來實現(xiàn)這樣一個功能。
    2015-04-04
  • python模擬登錄百度貼吧(百度貼吧登錄)實例

    python模擬登錄百度貼吧(百度貼吧登錄)實例

    python模擬登錄百度貼吧實例分享,大家參考使用吧
    2013-12-12
  • python+numpy實現(xiàn)的基本矩陣操作示例

    python+numpy實現(xiàn)的基本矩陣操作示例

    這篇文章主要介紹了python+numpy實現(xiàn)的基本矩陣操作,結(jié)合實例形式分析了Python使用numpy模塊針對矩陣進行創(chuàng)建、增刪查改、索引、運算相關操作實現(xiàn)技巧,注釋中包含有詳細的說明,需要的朋友可以參考下
    2019-07-07
  • python定時復制遠程文件夾中所有文件

    python定時復制遠程文件夾中所有文件

    這篇文章主要為大家詳細介紹了python定時復制遠程文件夾中所有文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • 詳解Python3中ceil()函數(shù)用法

    詳解Python3中ceil()函數(shù)用法

    在本篇內(nèi)容里我們給大家整理了關于Python3中ceil()函數(shù)用法以及相關知識點,需要的學習一下吧。
    2019-02-02
  • 探索Python?random模塊隨機性神奇世界

    探索Python?random模塊隨機性神奇世界

    Python中的random模塊提供了豐富的工具和函數(shù),幫助我們生成隨機數(shù)、操作隨機序列,以及模擬隨機性事件,在本文中,我們將分享random模塊,了解它的基本用法、功能和應用領域,并提供示例代碼來幫助你更好地理解隨機性的神奇世界
    2023-11-11
  • Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個程序,用于生成包含首頁、內(nèi)容頁和感謝頁的 PPTX 文檔,感興趣的小伙伴可以學習一下
    2023-08-08
  • python解析xml模塊封裝代碼

    python解析xml模塊封裝代碼

    這篇文章主要分享下在python中解析xml文件的模塊用法,以及對模塊封裝的方法,有需要的朋友參考下
    2014-02-02

最新評論

盐津县| 山阳县| 南雄市| 海盐县| 拜城县| 新竹市| 彰化市| 韶关市| 铁岭县| 本溪市| 象州县| 开封县| 杭锦旗| 武城县| 根河市| 甘孜县| 阿城市| 望谟县| 东乌珠穆沁旗| 深泽县| 彭泽县| 星座| 金溪县| 共和县| 和林格尔县| 青浦区| 万年县| 安义县| 梧州市| 麻阳| 瓦房店市| 炎陵县| 全椒县| 城口县| 龙州县| 新野县| 美姑县| 汕头市| 靖西县| 黄冈市| 焉耆|