最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python爬虫beta版之抓取知乎单页面

来源:动视网 责编:小采 时间:2020-11-27 14:17:11
文档

python爬虫beta版之抓取知乎单页面

python爬虫beta版之抓取知乎单页面:鉴于之前用python写爬虫,帮运营人员抓取过京东的商品品牌以及分类,这次也是用python来搞简单的抓取单页面版,后期再补充哈。#-*- coding: UTF-8 -*- import requests import sys from bs4 import BeautifulSoup #------知
推荐度:
导读python爬虫beta版之抓取知乎单页面:鉴于之前用python写爬虫,帮运营人员抓取过京东的商品品牌以及分类,这次也是用python来搞简单的抓取单页面版,后期再补充哈。#-*- coding: UTF-8 -*- import requests import sys from bs4 import BeautifulSoup #------知
 鉴于之前用python写爬虫,帮运营人员抓取过京东的商品品牌以及分类,这次也是用python来搞简单的抓取单页面版,后期再补充哈。

#-*- coding: UTF-8 -*- 
import requests
import sys
from bs4 import BeautifulSoup

#------知乎答案收集----------

#获取网页body里的内容
def get_content(url , data = None):
 header={
 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
 'Accept-Encoding': 'gzip, deflate, sdch',
 'Accept-Language': 'zh-CN,zh;q=0.8',
 'Connection': 'keep-alive',
 'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.235'
 }

 req = requests.get(url, headers=header)
 req.encoding = 'utf-8'
 bs = BeautifulSoup(req.text, "html.parser") # 创建BeautifulSoup对象
 body = bs.body # 获取body部分
 return body

#获取问题标题
def get_title(html_text):
 data = html_text.find('span', {'class': 'zm-editable-content'})
 return data.string.encode('utf-8')

#获取问题内容
def get_question_content(html_text):
 data = html_text.find('div', {'class': 'zm-editable-content'})
 if data.string is None:
 out = '';
 for datastring in data.strings:
 out = out + datastring.encode('utf-8')
 print '内容:
' + out
 else:
 print '内容:
' + data.string.encode('utf-8')

#获取点赞数
def get_answer_agree(body):
 agree = body.find('span',{'class': 'count'})
 print '点赞数:' + agree.string.encode('utf-8') + '
'

#获取答案
def get_response(html_text):
 response = html_text.find_all('div', {'class': 'zh-summary summary clearfix'})
 for index in range(len(response)):
 #获取标签
 answerhref = response[index].find('a', {'class': 'toggle-expand'})
 if not(answerhref['href'].startswith('javascript')):
 url = 'http://www.zhihu.com/' + answerhref['href']
 print url
 body = get_content(url)
 get_answer_agree(body)
 answer = body.find('div', {'class': 'zm-editable-content clearfix'})
 if answer.string is None:
 out = '';
 for datastring in answer.strings:
 out = out + '
' + datastring.encode('utf-8')
 print out
 else:
 print answer.string.encode('utf-8')


html_text = get_content('https://www.zhihu.com/question/43879769')
title = get_title(html_text)
print "标题:
" + title + '
'
questiondata = get_question_content(html_text)
print '
'
data = get_response(html_text)

输出结果:

文档

python爬虫beta版之抓取知乎单页面

python爬虫beta版之抓取知乎单页面:鉴于之前用python写爬虫,帮运营人员抓取过京东的商品品牌以及分类,这次也是用python来搞简单的抓取单页面版,后期再补充哈。#-*- coding: UTF-8 -*- import requests import sys from bs4 import BeautifulSoup #------知
推荐度:
标签: 知乎 python 爬虫
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top