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

鉴于之前用python写爬虫,帮运营人员抓取过京东的商品品牌以及分类,这次也是用python来搞简单的抓取单页面版,后期再补充哈。

#-*- coding: UTF-8 -*- import requestsimport sysfrom 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; WOW64) 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)

登录后复制

输出结果:

22.png

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。

发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2282371.html

(0)
上一篇 2025年2月27日 19:23:43
下一篇 2025年2月27日 19:23:56

AD推荐 黄金广告位招租... 更多推荐

相关推荐

  • python中round(x,[n])的使用

    round(x,[n]) 四舍五入 n保留小数部分个数 1、当不填写n时,默认为0,即不保留小数部分  a、当小数部分只为0.5时 整数部分如果为奇数则进1,如果为偶数则舍去小数部分  print(round(1.5))#2 小数部分为0.…

    编程技术 2025年2月27日
    100
  • python 数据类型 —字符串

    字符串去除空白 ,strip() , 包括空格,tab键, 换行符 >>> name = ”  Frank  “>>> name.strip()’Frank’ 登录后复制 字符串的分割, split(&#…

    编程技术 2025年2月27日
    200
  • python快速生成注释文档的方法

    python快速生成注释文档的方法  今天将告诉大家一个简单平时只要注意的小细节,就可以轻松生成注释文档,也可以检查我们写的类方法引用名称是否重复有问题等。一看别人专业的大牛们写的文档多牛多羡慕,不用担心我们可以让python为我们生成基本…

    2025年2月27日 编程技术
    200
  • python操作SQL

    pymsql是python中操作mysql的模块,其使用方法和mysqldb几乎相同 一、下载安装 pip3 install pymysql 二、操作使用 1、执行SQL 立即学习“Python免费学习笔记(深入)”; #!/usr/bin…

    2025年2月27日
    200
  • 通过reidis管理定时任务

    主要应用场景为:有变动需求的一次性定时任务。 通过redis过期事件的监听,执行相应命令。(注意:因为监听只能得到key, 所以需要另外存储具体执行内容体) 另外记得修改redis配置:notify-keyspace-events ex  …

    编程技术 2025年2月27日
    200
  • Python——将字符串转换为日期

    python中标准模块datetime能够将字符串转换为日期  from datetime import datetime   text = ‘2012-09-20’   y = datetime.strptime(…

    编程技术 2025年2月27日
    200
  • python——计算上周5的日期

    第一种方法:   from datetime import datetime, timedelta  weekdays = [‘Monday’,’Tuesday’,’Wednesday’,’Thursday’,              ‘…

    编程技术 2025年2月27日
    200
  • python java 调用

        jython    jython    2.1  登录后复制 package com.curiousby.python.demo;import org.python.core.PyFunction;import org.python…

    编程技术 2025年2月27日
    200
  • Python新手学习基础之数据类型——字符串的切片截取

    切片截取是python中字符串常见的一些操作,我们会在这篇文章里详细介绍,切片截取的作用是获取子字符或子字符串。 实际上,我们要做的就是使用索引,用冒号分隔两个索引,形式为:变量[头下标:尾下标],冒号之前的数字表示开始的位置,冒号之后的数…

    2025年2月27日
    200
  • Pycharm快捷键手册

    alt+enter 自动添加包 Ctrl+t SVN更新 Ctrl+k SVN提交 Ctrl + / 注释(取消注释)选择的行 Ctrl+Shift+F 高级查找 Ctrl+Enter 补全 Shift + Enter 开始新行 TAB S…

    编程技术 2025年2月27日
    200

发表回复

登录后才能评论