python登录知乎后怎样点击他人主页的更多按钮

我悄悄的发一个。用了我习惯的requests库,下载图片还是用的urllib,懒得改了,关键词给你添加了几个。你的问题主要在于对两个页面的headers检查是不一样的,下面代码中headers是访问‘更多页面’用的,headers2是访问回答页面用的。你要自己替换一下你的cookie# -*- coding: utf-8 -*-"""Created on Tue Aug 16 11:08:00 2016@author: pcloth"""import requests,timeimport re,osimport urllib.request as urfrom lxml import html#用时间戳取一页josn数据来检查,并获取符合条件的链接#每页数据有20个def get_check_this_page(timeStamp): global link,page_num print(\u0026#39;开始计算第%d页...时间戳:%s\u0026#39;%(page_num,timeStamp)) url = \u0026#39;https://www.zhihu.com/people/excited-vczh/activities\u0026#39; data_more = {\u0026#39;start\u0026#39;:timeStamp} response = requests.post(url,data=https://www.zhihu.com/api/v4/questions/50223370/data_more,headers=headers) if response.status_code!=200: return /u0026#39;打开页面错误。/u0026#39; ret_dict = eval(response.text) parsed_body = html.fromstring(ret_dict) href = parsed_body.xpath(/u0026#39;//a/u0026#39;) for a in href: if re.search(r/u0026#39;美女|女生|女朋友|胸|臀|腿|穿|身材|丝袜|美丽|好看|情趣|内衣/u0026#39;,a.text): url = a.get(/u0026#39;href/u0026#39;).replace(/u0026#39;/////u0026#39;,/u0026#39;/u0026#39;) title = a.text.replace(/u0026#39;//u0026#39;,/u0026#39;/u0026#39;) if /u0026#39;answer/u0026#39; not in url or link.get(/u0026#39;url/u0026#39;): pass else: print(title) link.update({url:title}) #查找下一页开始的时间戳 next_page = parsed_body.xpath(/u0026#39;//div/u0026#39;).values() next_time = next_page if len(next_time)==10 and type(next_time)==str: #time.sleep(3) page_num += 1 if page_num/u0026lt;=40: #一共获取多少页停止 get_check_this_page(next_time) else: returndef get_picture(): route = /u0026#39;D:////vczh_pic/u0026#39; headers2 = { /u0026#39;Accept/u0026#39;:/u0026#39;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8/u0026#39;, /u0026#39;Accept-Encoding/u0026#39;:/u0026#39;gzip, deflate, sdch, br/u0026#39;, /u0026#39;Accept-Language/u0026#39;:/u0026#39;zh-CN,zh;q=0.8/u0026#39;, /u0026#39;Cache-Control/u0026#39;:/u0026#39;max-age=0/u0026#39;, /u0026#39;Connection/u0026#39;:/u0026#39;keep-alive/u0026#39;, /u0026#39;Cookie/u0026#39;:用你自己的cookie, /u0026#39;Host/u0026#39;:/u0026#39;www.zhihu.com/u0026#39;, /u0026#39;Upgrade-Insecure-Requests/u0026#39;:/u0026#39;1/u0026#39;, /u0026#39;User-Agent/u0026#39;:/u0026#39;Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36/u0026#39;, } try: os.mkdir(route) except: pass finally: os.chdir(route) urls=list(link.keys()) for url in urls: this_url = /u0026#39;https://www.zhihu.com/u0026#39;+url response = requests.get(this_url,headers=headers2) if response.status_code!=200: return /u0026#39;打开页面错误。/u0026#39; parsed_body = html.fromstring(response.text) picture_src1 = parsed_body.xpath(/u0026#39;//img/@src/u0026#39;) picture_src2 = parsed_body.xpath(/u0026#39;//img/@src/u0026#39;) for pic in : if pic==/u0026#39;https://pic/u0026#39;: download_image_file(pic,route) #下载图片函数def download_image_file(imgUrl,local_dir): try: local_filename = imgUrl.split(/u0026#39;/u0026#39;) pic_res = ur.urlopen(imgUrl).read() print(/u0026#39;正在下载图片:%s/u0026#39;%local_filename) with open(local_dir+/u0026#39;/////u0026#39;+local_filename, /u0026#39;wb/u0026#39;) as f: f.write(pic_res) return local_filename except: print("下载图片错误。")def main(): global link,page_num,headers headers = { "Accept":"*/*", "Accept-Encoding":"gzip, deflate, br", "Accept-Language":"zh-CN,zh;q=0.8", "Connection":"keep-alive", "Content-Length":"16", "Content-Type":"application/x-www-form-urlencoded; charset=UTF-8", "Cookie":用你自己的cookie, "Host":"www.zhihu.com", "Origin":"https://www.zhihu.com", "Referer":"https://www.zhihu.com/people/excited-vczh", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36", "X-Requested-With":"XMLHttpRequest", "X-Xsrftoken":"8b79439c3f2ade7d44316fa6664a0e16", } page_num = 1 timeArray = time.localtime() timeStamp = str(int(time.mktime(timeArray))) link = {} get_check_this_page(timeStamp) get_picture() return link if __name__=="__main__": main()


推荐阅读