知乎对 selenium 有检测,不显示用户回答

更改 window.navigator.webdriver 属性绕过检测,即可正常显示用户回答。

profile = webdriver.FirefoxProfile()
profile.set_preference("dom.webdriver.enabled", False)

用户页面

driver.get('https://www.zhihu.com/people/Victue/answers')

问题及回答列表

QAs = driver.find_elements_by_class_name('List-item')

问题

QA.find_element_by_css_selector('h2.ContentItem-title>div>a').text

阅读全文按钮

QA.find_element_by_css_selector('div.RichContent-inner')

回答内容(粗暴的显示所有文字,后续考虑保存为富文本)

QA.find_element_by_css_selector('div.RichContent-inner').text

赞同数量

QA.find_element_by_css_selector('button.VoteButton').text

评论数量

QA.find_element_by_css_selector('button.ContentItem-action').text

发布时间

creat_time = QA.find_element_by_css_selector('div.ContentItem-time>a>span')
creat_time.get_attribute('data-tooltip')

下一页

next_page = driver.find_element_by_css_selector('button.PaginationButton-next')

# 第二种方式

答案列表中每个答案包含 mete 属性

# 赞同数
upvoteCount = QA.find_element_by_xpath('(//meta[@itemprop="upvoteCount"])[i]')
# 发布及编辑时间,此时间为 UTC
dateCreated = QA.find_element_by_xpath('(//meta[@itemprop="dateCreated"])[i]')
dateModified = QA.find_element_by_xpath('(//meta[@itemprop="dateModified"])[i]')
# 评论数
commentCount = QA.find_element_by_xpath('(//meta[@itemprop="commentCount"])[i]')
upvoteCount.get_attribute('content')
dateCreated.get_attribute('content')
dateModified.get_attribute('content')
commentCount.get_attribute('content')
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

CHNOTES 微信支付

微信支付

CHNOTES 支付宝

支付宝

CHNOTES 贝宝

贝宝