代码很简单,主要是需要自己在知乎官网登录一下获取到cookie,然后将cookie复制下来,放到request headers里面,去请求知乎热榜的时候把这个cookie带着即可。
不知道cookie有没有有效期,一般都会有的吧,如果失效了重新登录一下再获取一下咯。
忘了代码是从哪里荡的了,将就看看吧,哭了。
我发现我的正则学的是真的菜,完全不知道re模块是如何使用的,菜是原罪,哎!
代码如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import re
import requests
def main():
headers = {'user-agent':'Baiduspider',
'Cookie':'Cookie现身',
}
seed_url = 'https://www.zhihu.com/hot'
resp = requests.get(seed_url,
headers=headers)
html = resp.text
match_obj = re.compile(r'<h2 class="HotItem-title">.*?</h2>', re.S)
results = re.findall(match_obj, html)
for item in results:
match_title = r'<h2 class="HotItem-title">(.*?)</h2>'
title = re.findall(match_title, item)[0]
print(title)
if __name__ == '__main__':
main()
希望能有所帮助,那就是万幸了,谢谢!