Python-爬虫-爬取知乎的标题和当页显示的文字

# coding:utf-8
import requests
from bs4 import BeautifulSoup

quesNumStr = str(input("请输入搜索关键字:"))

url = 'https://www.zhihu.com/search?type=content&q='+quesNumStr

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'  # your user-Agent here
}

data = requests.get(url, headers=headers)
soup = BeautifulSoup(data.text, 'lxml')
liList = soup.select('li')
print(len(liList))
for li in liList:
    try:
        temp1 = li.select('a[class="js-title-link"]')
        if temp1:
            print('The title is :')
            print(temp1[0].get_text())
        temp2 = li.select('div[class="summary hidden-expanded"]')
        if temp2:
            print('The content is:')
            print(temp2[0].text)
    except:
        pass

 

posted on 2017-06-13 22:40  可豆豆  阅读(3578)  评论(0编辑  收藏  举报

导航