鱼C论坛

 找回密码
 立即注册
查看: 2350|回复: 3

[已解决]依然还是知乎某用户主页的“更多”按钮的问题

[复制链接]
发表于 2016-8-27 21:42:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
上回其实已经问过了,由@SixPy 大神给了一个简单的解答。当时觉得会了,所以就没再问。但这两天照之前的想法试了一下,发现失败了。
http://bbs.fishc.com/forum.php?m ... peid%26typeid%3D393
之前是发现每次点击“更多”的时候会POST一个start,这个start的值是他的主页上最后一条记录的'data-time'。

url

url

start

start

于是我就想着找到最后一个data-time,POST到那个url就行了,然后就写了如下代码
  1. import urllib.request as ur
  2. import urllib.parse as up
  3. from http.cookiejar import CookieJar
  4. import re
  5. from bs4 import BeautifulSoup
  6. import os
  7. import time

  8. url = 'http://www.zhihu.com/'

  9. #构造opener
  10. cj = CookieJar()
  11. opener = ur.build_opener(ur.HTTPCookieProcessor(cj))
  12. opener.addheader = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36')]

  13. #获取_xsrf
  14. def get_xsrf():
  15.     url = 'http://www.zhihu.com/'
  16.     response = opener.open(url)
  17.     html = response.read()
  18.     soup = BeautifulSoup(html,'html.parser')
  19.     val = soup.find(attrs = {'name':'_xsrf'})
  20.     value = val['value']
  21.     return value

  22. #登录知乎
  23. url2 = ''.join([url,'login/email'])
  24. data = {}
  25. data['_xsrf'] = get_xsrf()
  26. data['password'] = 密码
  27. data['captcha_type'] = 'cn'
  28. data['remember_me'] = 'true'
  29. data['email'] = 账号
  30. response2 = opener.open(url2,up.urlencode(data).encode('utf-8'))

  31. #轮子哥主页!
  32. url_lunzi = 'https://www.zhihu.com/people/excited-vczh'
  33. response3 = opener.open(url_lunzi)
  34. html2 = response3.read().decode('utf-8')
  35. soup2 = BeautifulSoup(html2,'html.parser')

  36. #寻找他赞过的答案
  37. answer = []
  38. while 1:
  39.     if len(answer) < 30:
  40.         a = soup2.find_all(name='div',
  41.                             class_="zm-profile-section-item zm-item clearfix")
  42.         start = a[-1]['data-time']
  43.         url_more = 'https://www.zhihu.com/people/excited-vczh/activities'
  44.         data_more = {'start':start}
  45.         response3 = opener.open(url_more, up.urlencode(data_more).encode('utf-8'))
  46.         html3 = response3.read().decode('utf-8')
  47.         soup3 = BeautifulSoup(html3,'html.parser')
  48.         allanswer = soup3.find_all(name = 'div',
  49.                                  class_ = "zm-profile-section-main zm-profile-section-activity-main zm-profile-activity-page-item-main",
  50.                                 )
  51.         for i in range(len(allanswer)-1,-1,-1):
  52.             if allanswer[i] in answer:
  53.                 del allanswer[i]
  54.             elif '赞同了回答' not in allanswer[i].span.next_sibling:
  55.                 del allanswer[i]
  56.         answer = allanswer
  57.     else:
  58.         break


  59. #下载图片
  60. route = 'D:\\vczh_pic'
  61. try:
  62.     os.mkdir(route)
  63. except:
  64.     pass
  65. finally:
  66.     os.chdir(route)
  67. for each in answer:
  68.     link = each.span.next_sibling.next_sibling['href']
  69.     link = ''.join(['http://www.zhihu.com',link])
  70.     res = opener.open(link).read().decode('utf-8')
  71.     soup = BeautifulSoup(res,'html.parser')
  72.     pic = soup.find_all(attrs = {'data-rawwidth':'1280','class':'origin_image zh-lightbox-thumb lazy'})
  73.     for each_pic in pic:
  74.         pic_name = re.sub(r'https://pic\d\.zhimg\.com/','',each_pic['data-actualsrc'])
  75.         with open(pic_name,'wb') as f:
  76.             src = each_pic['data-actualsrc']
  77.             pic_res = ur.urlopen(src).read()
  78.             f.write(pic_res)
复制代码


这个代码我分段测试过,登录和筛选记录以及下载图片都是正常的,问题还是出在了想点击“更多”时
下面是出的问题
  1. Traceback (most recent call last):
  2.   File "C:\Users\Administrator\Desktop\轮带逛.py", line 71, in <module>
  3.     response3 = opener.open(url_more, up.urlencode(data_more).encode('utf-8'))
  4.   File "D:\Python34\lib\urllib\request.py", line 469, in open
  5.     response = meth(req, response)
  6.   File "D:\Python34\lib\urllib\request.py", line 579, in http_response
  7.     'http', request, response, code, msg, hdrs)
  8.   File "D:\Python34\lib\urllib\request.py", line 507, in error
  9.     return self._call_chain(*args)
  10.   File "D:\Python34\lib\urllib\request.py", line 441, in _call_chain
  11.     result = func(*args)
  12.   File "D:\Python34\lib\urllib\request.py", line 587, in http_error_default
  13.     raise HTTPError(req.full_url, code, msg, hdrs, fp)
  14. urllib.error.HTTPError: HTTP Error 403: Forbidden
复制代码


实在是束手无策了,难道必须要用selenium才行吗?应该怎么修改呢?
最佳答案
2016-8-27 23:36:06
你翻页太快,zhihu有反爬虫机制~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-8-27 21:50:06 | 显示全部楼层
顺道问一句。。。BeautifulSoup的find功能是不是只能找最开始的一个,不能从后往前找,如果想找最后一个就必须find_all然后取-1?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-27 23:36:06 | 显示全部楼层    本楼为最佳答案   
你翻页太快,zhihu有反爬虫机制~
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-28 23:55:07 | 显示全部楼层
SixPy 发表于 2016-8-27 23:36
你翻页太快,zhihu有反爬虫机制~

感觉好像不是。。。我在else上面,也就是answer=allanswer那一句下面加了一句time.sleep(10),但还是报了一样的错误信息
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 02:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表