码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-06-13 23:47:35      阅读:953      评论:0      收藏:0      [点我收藏+]

标签:webkit   arc   https   app   .com   html   pass   htm   mozilla   

# 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

 

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

标签:webkit   arc   https   app   .com   html   pass   htm   mozilla   

原文地址:http://www.cnblogs.com/fredkeke/p/7003923.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!