Python爬虫入门教程 31-100 36氪(36kr)数据抓取 scrapy
2019-02-12 09:17
1191 查看
1. 36氪(36kr)数据----写在前面
今天抓取一个新闻媒体,36kr的文章内容,也是为后面的数据分析做相应的准备的,预计在12月底,爬虫大概写到50篇案例的时刻,将会迎来一个新的内容,系统的数据分析博文,记得关注哦~
36kr 让一部分人先看到未来,而你今天要做的事情确实要抓取它的过去。
2. 36氪(36kr)数据----数据分析
36kr的页面是一个瀑布流的效果,当你不断的下拉页面的时候,数据从后台追加过来,基于此,基本可以判断它是ajax异步的数据,只需要打开开发者工具,就能快速的定位到想要的数据,我们尝试一下!
捕获链接如下
https://36kr.com/api/search-column/mainsite?per_page=20&page=1&_=1543840108547 https://36kr.com/api/search-column/mainsite?per_page=20&page=2&_=1543840108547 https://36kr.com/api/search-column/mainsite?per_page=20&page=3&_=1543840108547 https://36kr.com/api/search-column/mainsite?per_page=20&page=4&_=1543840108547
在多次尝试之后,发现per_page最大可以扩展到300,但是当大于100的数据,返回的数据并不是很理想,所以,我们拟定为100即可,page就是页码,这个不断循环叠加即可。
上面的参数还有一个更加重要的值,叫做
total_count总共有多少文章数目。有这个参数,我们就能快速的拼接出来,想要的页码了。
3. 36氪(36kr)数据----创建scrapy项目
scrapy startproject kr36
4. 36氪(36kr)数据----创建爬虫入口页面
scrapy genspider Kr36 "www.gaokaopai.com"
5. 36氪(36kr)数据----编写url生成器
页面起始地址
start_urls为第一页数据,之后会调用
parse函数,在函数内容,我们去获取
total_count这个参数
这个地方,需要注意
yield返回数据为
Request()关于他的详细说明,请参照
https://scrapy-chs.readthedocs.io/zh_CN/0.24/topics/request-response.html
所有参数清单,参数名字起得好,基本都能代表所有的意思了。比较重要的是
url和
callback
class scrapy.http.Request(url[, callback, method='GET', headers, body, cookies, meta, encoding='utf-8', priority=0, dont_filter=False, errback])
class Kr36Spider(scrapy.Spider): name = 'Kr36' allowed_domains = ['36kr.com'] start_urls = ['https://36kr.com/api/search-column/mainsite?per_page=100&page=1&_='] def parse(self, response): data = json.loads(response.body_as_unicode()) totle = int(data["data"]["total_count"]) #totle = 201 for page in range(2,int(totle/100)+2): print("正在爬取{}页".format(page),end="") yield Request("https://36kr.com/api/search-column/mainsite?per_page=100&page={}&_=".format(str(page)), callback=self.parse_item)
6. 36氪(36kr)数据----解析数据
在解析数据过程中,发现有时候数据有缺失的情况发生,所以需要判断一下
app_views_count,
mobile_views_count,
views_count,
favourite_num是否出现在字典中。
注意下面代码中的Kr36Item类,这个需要提前创建一下
Kr36Item
class Kr36Item(scrapy.Item): # define the fields for your item here like: # name = scrapy.Field() app_views_count = scrapy.Field() # APP观看数量 mobile_views_count = scrapy.Field() # 移动端观看数量 views_count = scrapy.Field() # PC观看数量 column_name = scrapy.Field() # 类别 favourite_num = scrapy.Field() # 收藏数量 title = scrapy.Field() # 标题 published_at = scrapy.Field() # 发布时间 is_free = scrapy.Field() # 是否免费 username = scrapy.Field()
def parse_item(self,response): data = json.loads(response.body_as_unicode()) item = Kr36Item() for one_item in data["data"]["items"]: print(one_item) item["app_views_count"] = one_item["app_views_count"] if "app_views_count" in one_item else 0# APP观看数量 item["mobile_views_count"] = one_item["mobile_views_count"] if "mobile_views_count" in one_item else 0 # 移动端观看数量 item["views_count"] = one_item["views_count"] if "views_count" in one_item else 0 # PC观看数量 item["column_name"] = one_item["column_name"] # 类别 item["favourite_num"] = one_item["favourite_num"] if "favourite_num" in one_item else 0 # 收藏数量 item["title"] = one_item["title"] # 标题 item["published_at"] = one_item["published_at"] # 发布时间 item["is_free"] = one_item["is_free"] if "is_free" in one_item else 0# 是否免费 item["username"] = json.loads(one_item["user_info"])["name"] yield item
最后打开
settings.py中的
pipelines编写数据持久化代码
ITEM_PIPELINES = { 'kr36.pipelines.Kr36Pipeline': 300, }
import os import csv class Kr36Pipeline(object): def __init__(self): store_file = os.path.dirname(__file__)+'/spiders/36kr.csv' self.file = open(store_file,"a+",newline="",encoding="utf_8_sig") self.writer = csv.writer(self.file) def process_item(self, item, spider): try: self.writer.writerow(( item["title"], item["app_views_count"], item["mobile_views_count"], item["views_count"], item["column_name"], item["favourite_num"], item["published_at"], item["is_free"], item["username"] )) print("数据存储完毕") except Exception as e: print(e.args) def close_spider(self,spider): self.file.close()
7. 36氪(36kr)数据----获取数据
运行上述代码,没有做过多的处理,也没有调整并发速度,也没有做反爬措施。跑了一下,大概获取到了
69936条数据,和预估的差了300多条,问题不大,原因没细查,哈哈哈哈
相关文章推荐
- Python爬虫入门教程 30-100 高考派大学数据抓取 scrapy
- Python爬虫入门教程 23-100 石家庄链家租房数据抓取
- Python爬虫入门教程 21-100 网易云课堂课程数据抓取
- Python爬虫入门教程 29-100 手机APP数据抓取 pyspider
- Python爬虫入门教程 27-100 微医挂号网专家团队数据抓取pyspider
- Python爬虫入门教程 24-100 微医挂号网医生数据抓取
- Python爬虫入门教程 22-100 CSDN学院课程数据抓取
- Python爬虫入门教程 28-100 虎嗅网文章数据抓取 pyspider
- Python爬虫入门教程 16-100 500px摄影师社区抓取摄影师数据
- Python爬虫入门教程 17-100 博客抓取数据
- Python爬虫入门教程 18-100 煎蛋网XXOO图片抓取
- Python爬虫入门教程 3-100 美空网数据爬取
- Python爬虫入门教程 20-100 慕课网免费课程抓取
- Python爬虫入门教程 19-100 51CTO学院IT技术课程抓取
- Python爬虫入门教程 15-100 石家庄政民互动数据爬取
- [Python爬虫] 之二十一:Selenium +phantomjs 利用 pyquery抓取36氪网站数据
- Python爬虫框架Scrapy基本用法入门教程
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程
- Python爬虫入门教程 25-100 知乎文章图片爬取器之一
- [Python]网络爬虫(12):爬虫框架Scrapy的第一个爬虫示例入门教程