手记

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

# -*- 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',''))
0人推荐
随时随地看视频
慕课网APP