from urllib import request, parse
from html.parser import HTMLParser
import json
from idna import unicode
class MyHTMLParser(HTMLParser):
def __init__(self):
self.__xsrf = 0 # 爬取知乎,登录需要这个随机的token
HTMLParser.__init__(self)
def handle_startendtag(self, tag, attrs):
if tag == 'input':
try:
if attrs[0][1] == 'hidden' and attrs[1][1] == '_xsrf':
self.__xsrf = attrs[2][1]
except Exception as e:
pass
def handle_starttag(self, tag, attrs):
pass
def handle_data(self, data):
pass
@property
def xsrf(self):
return self.__xsrf
if __name__ == '__main__':
myparser = MyHTMLParser()
url = 'https://www.zhihu.com/signin'
req = request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0
python日常学习笔记4--模拟登陆知乎
最新推荐文章于 2024-08-21 23:42:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)