wnma3mz / wechat_articles_spider Goto Github PK
View Code? Open in Web Editor NEW微信公众号文章的爬虫
License: Apache License 2.0
微信公众号文章的爬虫
License: Apache License 2.0
您好,请问现在可以爬取历史消息的阅读数和点赞数吗?
@wnma3mz 你好,感谢你的code,非常clean,很喜欢!
在你的博客上看到说从公众号获取永久链接时,间隔3分钟,可以连续获取几小时
有三个问题:
not use mitmproxy
Traceback (most recent call last):
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 662, in urlopen
self._prepare_proxy(conn)
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 948, in prepare_proxy
conn.connect()
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connection.py", line 352, in connect
self.sock = ssl_wrap_socket(
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\ssl.py", line 370, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\util\retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mp.weixin.qq.com', port=443): Max retries exceeded with url: /mp/getappmsgext?appmsg_token=1060_7%2F4I3ZMFTPIxjL%2B52tKBp1IXwiPj_emVvCB2UrVeXEcnQE3tlji-iAPnHtzaUyzgf1bzhDrJAeH3G0_r&x5=0 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\wechat_articles_spider\wechatarticles\ArticlesInfo.py", line 72, in read_like_nums
appmsgstat = self.__get_appmsgext(article_url)["appmsgstat"]
File "D:\wechat_articles_spider\wechatarticles\ArticlesInfo.py", line 213, in __get_appmsgext
appmsgext_json = requests.post(
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Admin-Jumper\AppData\Local\Programs\Python\Python38\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='mp.weixin.qq.com', port=443): Max retries exceeded with url: /mp/getappmsgext?appmsg_token=1060_7%2F4I3ZMFTPIxjL%2B52tKBp1IXwiPj_emVvCB2UrVeXEcnQE3tlji-iAPnHtzaUyzgf1bzhDrJAeH3G0_r&x5=0 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/wechat_articles_spider/test/test_WechatInfo.py", line 16, in
read_num, like_num = test.read_like_nums(article_url)
File "D:\wechat_articles_spider\wechatarticles\ArticlesInfo.py", line 75, in read_like_nums
raise Exception("params is error, please check your article_url")
Exception: params is error, please check your article_url
Process finished with exit code 1
你好,我最近也在做一个类似的爬虫。
采取的思路和你的是一致的,通过登录一个自己的个人公众账号,获取到cookies和token之后,通过接口去获取某一个公众号的文章列表。
但是这个每次登录,都需要人工干预。我尝试把公众平台登录界面的二维码发送到手机,然后长按识别,这样不能达到想要的效果。
请问你这边有可以不需要人工盯着的登录方式吗?
你好,请问:
通过os模块执行 mitmdump命令时 提示找不到,使用subprocess模块发现也无法找到mitmdump,有什么办法能够解决的吗
获取文章url接口用多了之后,就一直不能用了,要多久能恢复啊
下载下来之后没有makefile,pip install 也不可以想问一下,如何使用工程呢?直接跑test是找不到对应模块的。
ArticlesUrls
的 __save_login_qrcode
并不会因为打开图片而暂停,这样会导致程序反复尝试重新登录并且打开一个新的二维码...
目前想到的解决方法是使用 OpenCV
的 cvWaitKey
方法来暂停,或者将 __login_official
方法最后调用的 __startlogin_official
方法改为自调用,并加入休眠时间。
最后说一句,大佬NB!
您好,根据代码我已经拿到文章的url了。下一步想把文章存到word里,但是拿不到图片的src,咋解决呢。
OfficialWeChat和LoginWeChat在哪里。。
、
基本上token和cookie一周到期一次,需要在web页面重新扫码登录 这个token应该是与手机微信关联的。跨过手动操作你解决了吗?
之前测试使用的自己微信号,但当真正爬取时提示更新key,但key已经更新,这是否意味着被封ip或者什么?那被封的时长是多久
首先感谢作者的思路和代码
但是目前的获取点赞量与阅读量的接口好像和代码中的不一样,代码中为:
appmsgext_url = origin_url + "__biz={}&mid={}&sn={}&idx={}&appmsg_token={}&x5=1".format(
biz, mid, sn, idx, self.appmsg_token)
而我通过抓包得到getappmsgext?f=json&mock=&uin=NjY0NzM2Njgw&key...
另外,好像并不是appmsg_token 正确就有效的,具体的机制我也没分析出来,还请作者解答
没有查找文章的按钮了
wechat_articles_spider/wechatarticles/ArticlesUrls.py中的注释如下:
def get_urls(self, nickname=None, biz=None, begin=0, count=5):
"""
获取公众号的每页的文章信息
Parameters
----------
nickname : str
需要爬取公众号名称
biz : str
需要爬取公众号的biz, 优先
begin: str or int
起始爬取的页数
count: str or int
每次爬取的数量,1-5
......
此处的begin应该指的是从历史文章列表第几项开始的索引
如题,我用这个延时1分钟都会被封。奇怪了
我目前的项目用selenium模拟+扫码登录,大约20小时左右会出现
{"base_resp":{"ret":200003,"err_msg":"invalid session"}}
就得重新扫码登录公众号了
您好,最新我也在研究微信公众号爬取的问题,正好看到了您的项目。有一个问题还想请教下您:
获取二维码的url中每次会有一个rd参数,每次是不一样的,在您的项目中,我看您是把rd参数写死了。我找了很久rd参数的来源,感觉应该是某个js文件执行的结果,但是并没有找到,请问您研究过这个rd参数吗?
你好楼主,非常感谢您写的项目,可以加微信交流下嘛,谢谢
微信PC安装在xp上,并在登录界面设了代理,指向 mitmdump 的 ip 和端口;mitmdump 在另一台机器上运行
在 xp 上导入 mitmdump 生成的证书
在 xp 上运行并登录微信,mitmdump 打印大量的错误信息,比如:
192.168.10.102:1869: CONNECT 113.96.209.105:443
<< HTTP protocol error in client request: Bad HTTP request line: b'\x16\xf1\x03\x00\xa1\x00\x00 .....
192.168.10.102:1863: CONNECT 14.17.73.39:80
<< HTTP protocol error in client request: Bad HTTP request line: b"\xab\x00\x00\x01\x03'\ ....
一直找不到发向 mp.weixin.qq.com/mp/
的流量。会是证书的问题吗?
在 xp 上运行 fiddler,抓取是正常的
尝试在pycharm中进行debug的时候,二维码会不断出现,扫描登陆了也没办法继续下去,不知道大家有没有好的解决方法呢,感谢!
第61行,download_img 方法中如果image 已经存在,返回的替换imageurl是以全路径替换的。而第一次替换的时候是以basename路径拼接的,意味着用相同image的第一次和第二次是不同的路径,导致同一源的image后续使用的时候会路径不对,可以改成统一的。
作者您好,我们也是一家专业做IP代理的服务商,极速HTTP,想跟您谈谈是否能够达成商业推广上的合作。如果您,有意愿的话,可以联系我,微信:13982004324 谢谢(如果没有意愿的话,抱歉,打扰了)
appmsg_token用同一个微信号阅读不同微信公众号的文章时似乎值不一样,有办法自动获取吗?半天内就失效?如果我想用一个微信号看100个微信公众号文章的阅读数似乎做不到
对于获取微信文章链接,提供一种思路,就是通过wx小助手 将最新文章的推送转发出来
我改了下test_WechatUrls.py,用自己注册的公众号的token和cookies,想抓大概20多个公众号8月份至今的所有文章url,之前试过直接每个号抓50个,第一遍好像能抓完,结果没保存下来,后来再抓就报cookie或token错误了,过了一天我改成每个号只抓5个的话也差最后3个公众号抓不到报错,不过我发现有些好设置抓5个,但返回了7-10个文章,还有我用create_time转datetime发现和点开url网页页面上的时间不一致。
主要是有没有啥办法能不被反爬ban掉,可以抓的慢一点,不太方便再搞更多的公众号了。
个人微信号登陆后获取的key,隔段时间更新,这个key应该在哪里获得
尝试了自己制作了Selenium的脚本用公众号图文推送的方案爬取,但大概到500条的时候,下一页按钮将会触发“系统错误”提示,页面将不再返回更多历史文章信息,想问下微信是否有在这个接口上设置单账号单日上限?
p.s.我的pc版本微信历史文章页面查看也显示访问异常,根本无法打开了。
使用您方案中提供的test_getURL.py测试,仍然遇到同样问题,爬取一段时间后即不再返回更多。
想咨询下是否有解决方案?非常感谢
如题.
首先感谢分享!
在运行代码的时候发现utils.py第189行timestamp2date(dt)报错,缺少一个格式化输入。阅读源码后认为这段代码可以删去,或者给定一个日期格式化,否则会在爬取第一轮(10次)的时候报错中断,删去这行代码后代码运行正常。
此外,给出的demo test_GetUrls.py中,84-88行中的url并没有定义(不过可能就是这样写防小白的?)
第27行item=line赋值可以删去,第24行改为 for i, line in enumerate(lst, 0):
同时,第56行需要判断flag是否为1,而这时会报错flag没有定义。应在前面定义flag函数,如在24行加入flag = 0
根据get_history_urls返回的数据格式,应在24行lst遍历前,加入一个遍历,如:
def demo(lst_list): fj = "" item_lst = [] flag = 0 for lst in lst_list: for i, item in enumerate(lst, 0):
这样在demo中才能正确读取传入的数据,否则会报错。
还有源码53-54行的
finally: save_xlsx(fj, item_lst)
可以删去
感谢代码的分享,节省了很多造轮子的功夫。
非常感谢开发这个工具,参考 https://github.com/wnma3mz/wechat_articles_spider/blob/master/test/test_ArticlesAPI.py 我爬取的时候发现会连着跳过几天的推文~不知道有什么解决办法没有。
我检查了下代码应该没有问题,会连着获取几天的推文,然后又跳过几天的推文。。。
# 自定义爬取,每次爬取5篇以上
start = 0
count = 10 # 不是每次得到的都一样
time_delay = 60 * 3
for i in range(100):
if i != 0:
start += len(data)
print("===============")
print("Query round: " + str(i))
print("Start set to: %d" % start)
print()
data = loop_query(test, nickname, start, count)
with open('out.csv', 'a') as f:
for j in range(len(data)):
print("Writing wechat post: " + data[j]['title'])
f.write(data[j]['title'] + ',' + data[j]['link'] + '\n')
比如这篇文章的coment_id的获取,无法用“ArticlesInfo”类中的“__get_comment_id”方法获取到,这个comment_id的样式是这样的:
<html>
<body>
<!--StartFragment-->
d.article_title = xml ? getXmlValue('article_title.DATA') : '';
--
| d.comment_id = xml ? getXmlValue('comment_id.DATA') : '1767689550690091015';
<!--EndFragment-->
</body>
</html>
建议增加选项,在传统方式无法获取时,用第二种方式获取一下。
我更改的代码如下,供参考哈:
def __get_comment_id(self, article_url):
"""
获取comment_id
Parameters
----------
article_url: str
文章链接
Returns
-------
str:
comment_id获取评论必要参数
"""
res = self.s.get(article_url, data=self.data, proxies=self.proxies)
# 使用正则提取comment_id
comment_id = re.findall(r'comment_id = "\d+"', res.text)
#如果上一步获取的comment_id为空,则尝试第二种方法获取,如果上一步已经获取了,就跳过
if len(comment_id) == 0:
comment_id = re.findall(r"(?<=comment_id.DATA\'\)\s\:\s\')[0-9]+",res.text)
return comment_id[0]
if len(comment_id) > 0:
return comment_id[0].split(" ")[-1][1:-1]
return ""
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.