当前位置: 移动技术网 > IT编程>脚本编程>Python > Python爬虫入门教程 25-100 知乎文章图片爬取器之一

Python爬虫入门教程 25-100 知乎文章图片爬取器之一

2019年01月17日  | 移动技术网IT编程  | 我要评论
1. 知乎文章图片写在前面 今天开始尝试爬取一下知乎,看一下这个网站都有什么好玩的内容可以爬取到,可能断断续续会写几篇文章,今天首先爬取最简单的,单一文章的所有回答,爬取这个没有什么难度。 找到我们要爬取的页面,我随便选了一个 个回答,数据量可以说非常小了,就爬取它吧。 2. 知乎文章图片选取操作库 ...

1. 知乎文章图片写在前面

今天开始尝试爬取一下知乎,看一下这个网站都有什么好玩的内容可以爬取到,可能断断续续会写几篇文章,今天首先爬取最简单的,单一文章的所有回答,爬取这个没有什么难度。

找到我们要爬取的页面,我随便选了一个

https://www.zhihu.com/question/292393947

在这里插入图片描述

1084个回答,数据量可以说非常小了,就爬取它吧。

2. 知乎文章图片选取操作库和爬取地址

爬取使用requests 存储使用 mongodb 就可以了

爬取地址经过分析之后,找到了一个可以返回json的数据接口

在这里插入图片描述

提取链接,看一下各参数的意思,方便我们程序模拟

https://www.zhihu.com/api/v4/questions/292393947/answers?include=data%5b%2a%5d.is_normal%2cadmin_closed_comment%2creward_info%2cis_collapsed%2cannotation_action%2cannotation_detail%2ccollapse_reason%2cis_sticky%2ccollapsed_by%2csuggest_edit%2ccomment_count%2ccan_comment%2ccontent%2ceditable_content%2cvoteup_count%2creshipment_settings%2ccomment_permission%2ccreated_time%2cupdated_time%2creview_info%2crelevant_info%2cquestion%2cexcerpt%2crelationship.is_authorized%2cis_author%2cvoting%2cis_thanked%2cis_nothelp%3bdata%5b%2a%5d.mark_infos%5b%2a%5d.url%3bdata%5b%2a%5d.author.follower_count%2cbadge%5b%2a%5d.topics&limit=5&offset=10&sort_by=default

上面的连接进行了url编码,去找个解码工具解析一下,编程下面的url就比较好解释了,answers后面跟了一堆的参数,应该是返回的关键字,找到limit每页显示的数据量,offset偏移量,我们下拉滚动条,发现这个在不断的叠加+5,sort_by 就是排序。

https://www.zhihu.com/api/v4/questions/292393947/answers?include=data[*].is_normal,admin_closed_comment,reward_info,is_collapsed,annotation_action,annotation_detail,collapse_reason,is_sticky,collapsed_by,suggest_edit,comment_count,can_comment,content,editable_content,voteup_count,reshipment_settings,comment_permission,created_time,updated_time,review_info,relevant_info,question,excerpt,relationship.is_authorized,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics&limit=5&offset=10&sort_by=default

做好上面的工作,接下来就是爬取了,我简化了一下爬取的地址,只保留了一些关键的信息

  https://www.zhihu.com/api/v4/questions/292393947/answers?include=comment_count,content,voteup_count,reshipment_settings,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics&limit=5&offset=0&sort_by=default

3. 知乎文章图片编写代码

分析完毕之后,发现代码非常简单了

import requests
from fake_useragent import useragent

############## 数据存储
import pymongo
import time
database_ip = '127.0.0.1'
database_port = 27017
database_name = 'sun'
client = pymongo.mongoclient(database_ip,database_port)
db = client.sun
db.authenticate("dba", "dba")
collection = db.zhihuone  # 准备插入数据

##################################

class zhihuone(object):

    def __init__(self,totle):

        self._offset = 0
        self._totle = totle
        #self._ua = useragent()


    def run(self):

        print("正在抓取 {} 数据".format(self._offset))
        headers = {
            "upgrade-insecure-requests":"1",
            "user-agent": "mozilla/5.0 (windows nt 10.0; wow64)"
        }
        with requests.session() as s:
            try:
                with s.get("https://www.zhihu.com/api/v4/questions/292393947/answers?include=comment_count,content,voteup_count,reshipment_settings,is_author,voting,is_thanked,is_nothelp;data[*].mark_infos[*].url;data[*].author.follower_count,badge[*].topics&limit=5&offset={}&sort_by=default".format(self._offset),headers=headers,timeout=3) as rep:
                    data =  rep.json()
                    if data:
                        collection.insert_many(data["data"])
            except exception as e:
                print(e.args)

            finally:
             
                if self._offset <= self._totle:
                    self._offset  = self._offset + 5 # 每次+5
                    print("防止被办,休息3s")
                    time.sleep(3)
                    self.run()
                else:
                    print("所有数据获取完毕")




if __name__ == '__main__':
    # 偏移量是0,5,10   i=1  (i-1)*5
    zhi = zhihuone(1084)  
    zhi.run()

上面主程序入口中,我写了个1084 ,这个偷懒,就硬编码了,数据当然也可以通过爬取获取,没有任何问题

4. 知乎文章图片写在后面

本篇文章是知乎文章爬取器之一,接下来完善的功能

  1. 爬取地址用户可以输入
  2. 自动答案总数
  3. 文章中图片自动下载
  4. 等功能

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网