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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM