继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

爬取知乎单个网页问题和回答

慕粉4051712
关注TA
已关注
手记 1
粉丝 1
获赞 2
# -*- coding: utf-8 -*-

import urllib
import urllib.request
from bs4 import BeautifulSoup

url='https://www.zhihu.com/question/19763541'
request = urllib.request.Request(url)
response = urllib.request.urlopen(url)
page = response.read().decode('utf-8')
soup = BeautifulSoup(page,'html5lib')
# 问题是什么
question = soup.find_all('h2')[0]
print(question.find('span').string)

# 赞最多的回答
all_answer = soup.find_all('div',class_='zm-item-answer zm-item-expanded')
goodAnswer = 0
bestAnswer = 0
for answer in all_answer:
    goodAnswer = answer.find('span',class_='count').string
    #if(goodAnswer > bestAnswer):
    #    bestAnswer = goodAnswer
    answerText = answer.find('div',class_='zm-editable-content clearfix')

    print(goodAnswer +' '+ answerText.text[:10].replace('\n',''))
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP