微信公众号文章的爬取(搜狗微信搜索)
1.模拟浏览器登陆,获取cookies
2.request.get()带上cookies访问
3.反反爬虫(待定,目前是代理ip+休眠,搜狗模式:封ip+封cookie+重点关照[我这里有一句mmp一定要讲])
附上勉强能用的代码(自己根据实际情况,选择代理ip和休眠时间)
PS:获取代理ip代码:gei_ip_pools在置顶文章里面
from selenium import webdriver
import requests
import time
from bs4 import BeautifulSoup
import re
from mysql_py import *
import threading
from urllib import request
from get_ip_pools import *
import random#get cookie
def get_cookies():driver = webdriver.Chrome()driver.get("http://weixin.sogou.com/")driver.find_element_by_xpath('//*[@id="loginBtn"]').click()time.sleep(10)cookies = driver.get_cookies()cookie = {}for items in cookies:cookie[items.get('name')] = items.get('value')return cookie#url = "http://weixin.sougou.com"
#response = requests.get(url,cookies = cookie)
#search = input("输入你想搜索的关键词")#get total url
def get_total_url(url):if url.startswith("//"):url = "http:" + urlelif url.startswith("/"):url = "http:/" + urlelse:url = urlreturn url#init header
header = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8','Accept-Encoding':'gzip, deflate','Accept-Language':'zh-CN,zh;q=0.9','Connection':'keep-alive','Host':'weixin.sogou.com','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',}#init proxys
alright_proxys = main_function()#get total page num
def get_page_count(search,cookie):global headerpage_source = requests.get("http://weixin.sogou.com/weixin?query=%s&type=2&page=1"%search,cookies = cookie,headers = header,proxies = alright_proxys[random.randint(0,len(alright_proxys)-1)]).contentbs_obj = BeautifulSoup(str(page_source,encoding = "utf-8"),"html.parser")#print(bs_obj)item_count_str = bs_obj.find("div",{"class":"mun"}).textpattern = re.compile(r'\d+')total_item_count = pattern.findall(item_count_str.replace(",",""))[0]page_count = int(int(total_item_count)/10)return page_count#check path
def check_mkdir(path):if not os.path.exists(path):try:os.makedirs(path)except Exception:pass#download img
def get_img(url,num,connect,cursor):global alright_proxysresponse = request.get(url,headers = header).contentcontent = str(response,encoding = "utf-8")bs_obj = BeautifulSoup(content,"html.parser")img_list = bs_obj.findAll("img")count = 0for img in img_list:try:imgurl=get_total_url(img.attrs["data-src"])store_name = "%s"%url_num+"%s"%countpath = r"C:\Users\Mr.Guo\Pictures\weixin"check_mkdir(path)#urllib.request.urlretrieve(imgurl,r"C:\Users\Mr.Guo\Pictures\weixin\%s.jpeg" %store_name)insert_into_table(connect,cursor,store_name,html)count += 1time.sleep(5)except Exception as e:pass#main function
def main_fun(page_count,search,cookie,connect,cursor):global headerfor i in range(page_count):num = ipage_source = requests.get("http://weixin.sogou.com/weixin?query=%s&type=2&page=%s"%(search,num + 1),cookies = cookie,headers = header,proxies = alright_proxys[random.randint(0,len(alright_proxys)-1)]).contentbs_obj = BeautifulSoup(str(page_source,encoding = "utf-8"),"html.parser")url_list = bs_obj.findAll("div",{"class":"txt-box"})final_url_list = []for url in url_list:final_url_list.append(url.h3.a.attrs['href'])for url_num in range(len(final_url_list)):t = threading.Thread(target = get_img,args = (final_url_list[url_num],url_num,connect,cursor))#time.sleep(3)t.start()
-----------------------调用,一部分是淘宝店铺的,同样在置顶文章中------------------------分割线:两部分代码
from final_test import *
from mysql_py import *
import threading
from sougou_wechat import *
#choice
choice = input('''输入需要获取的数据:a.淘宝店铺b.公众号文章''')
if choice == 'a':db,db_cursor = init_fun("taobao")check_tab_exist(db,db_cursor)href_list = get_item_href()for i in range(len(href_list)):start_url = href_list[i]get_shop_url(store_list,start_url)for shop_url in store_list:print(shop_url)t = threading.Thread(target = get_img_url,args = (shop_url,db,db_cursor))t.start()#t.join()#阻塞待定elif choice == 'b':db,db_cursor = init_fun("weixin")check_tab_exist(db,db_cursor)my_cookie = get_cookies() search = input("输入你想搜索的关键词")page_num = get_page_count(search,my_cookie)main_fun(page_num,search,my_cookie,db,db_cursor)
插入了对数据库的操作,可以自己选择性保留
微信公众号文章的爬取(搜狗微信搜索)相关推荐
- python实现微信hook_GitHub - gemgin/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...
wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...
- python实现微信hook_GitHub - redtips/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...
wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...
- python实现微信hook_GitHub - zhouxionger/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...
wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...
- python hook微信_GitHub - 15993248973/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...
wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...
- hook微信 python_GitHub - zkqiang/wechathook: 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取...
wechathook 借助微信hook,拦截修改某些call,填充进我们的Python代码,进行微信公众号文章的爬取 注入器 注入dll进程序中 DLL 实现hook功能,申请内存,修改call,在里 ...
- 微信公众号文章采集 爬取微信文章 采集公众号的阅读数和点赞数?
获取微信key工具:fiddler2+phpstudy 本文介绍的是获取的是公众号key,不是万能微信key.不过思路类似. 我已经找了微信万能key的方法,不过是万万不能说的,嘿嘿 phpstudy ...
- python微信公众号爬虫_Python爬取指定微信公众号所有文章!
篇文章使用到的技术: mitmdump + 电脑版微信 先分析打开可视化抓包工具, 勾选https代理.然后打开电脑版微信 任意点击一个公众号,再点击查看历史消息 注:想学习Python的小伙伴们 可 ...
- 【爬虫】【python】微信公众号信息的爬取(以南中医青年为例)
微信公众号信息的爬取 1.Selenium库来获取源码 打开搜狗微信,在搜索框中输入"南中医青年",这里就搜索到了南中医青年发布的公众号文章结果 但是在浏览器中复制浏览器地址后再度 ...
- python爬取公众号文章_python爬取微信公众号历史文章
前几天,朋友有一个爬取某些指定微信公众号的历史文章的需求,刚好自己闲的没事,也就试了一试.不算完美解决问题了吧,但是自己也无能为力了,毕竟腾讯那么牛,斗不过斗不过. 一.思路 看了一些别人的文章,综合 ...
- 基于搜狗接口的微信公众号及其信息爬取
1.思路 经测试,搜狗搜索提供的微信公众号的接口是理所当然爬取多了会被封ip等方式重点照顾.这只是做一个公众号及其链接的爬取,公众号的内容爬取及制定内容的爬取都是一个路子.搞懂了一个其他的就都差不多了 ...
最新文章
- 离散傅里叶变换(DFT)(为了使用而学习的DFT)
- 查询局域网内在线电脑IP
- Python官方文档学习心得(第二篇)
- 设备 esp32_低功耗ESP32手持式袖珍显示屏
- PHP爬取网页内容并存放至项目文件中
- 数电educoder的verilog参考答案
- SQL替换字段中一部分字符串
- 腾讯云部署node mysql_【腾讯云】配置安装node
- 小武实习的debug日记2
- 32bit MCU 与 16bit MCU 的 区别
- 3D打印将对零售模式产生颠覆影响,能否抓住机遇
- 储存数据与操作Excel [爬虫专题(9)]
- IO IR 个人作业汇总——康熙
- 学习没有动力的解决方法
- 空字符串、NULL、空格串的区别
- Prometheus断电启动异常
- 基于微信小程序的图书馆座位预约系统的设计与实现
- winform连接blynk 控制开发板
- 孤岛模式微电网逆变器VSG控制_SIMULINK_模型搭建详解
- 「 硬核教学」 ❤️ C语言编写扫雷游戏外挂❤️「 完整源码」
热门文章
- KSO-.NETCore部署CentOS7.5中验证码绘图Bitmap类型报错“The type initializer for ‘Gdip‘ threw an exception ”
- 学数据结构堆襸_如何编拼音输入的程序!(300分)
- 具体案例 快速原型模型_快速原型模型
- 升级IOS15.4出现更新验证失败如何解决
- 如何配置服务器的安全?
- python 断言方法_Python3断言
- android 行居中,android自己定义换行居中CenterTextView(示例代码)
- uniapp 打开第三方APP
- 根据时间经纬度高程计算天顶角
- 数据分析--卡方分析与方差分析