python3 爬取36氪新聞網(wǎng)頁

一個做了反爬的36氪,返回數(shù)據(jù)惡心,感覺是一堆垃圾。這里只是記錄一下爬取過程。

(一)爬取環(huán)境

  • win10
  • python3
  • scrapy

(二)爬取過程

(1)入口:搜索

image.png

(2)動態(tài)js數(shù)據(jù)加載,查看下一頁操作:

image.png

(3)返回數(shù)據(jù):

image.png

(4)請求鏈接

http://36kr.com/api//search/entity-search?page=4&per_page=40&keyword=機器人&entity_type=post&ts=1532794031142&_=1532848230039
分析:這里的ts及后面的都為時間戳格式,可不要,entity_type=post這個是必須要的,可變參數(shù)為page

(4)列表頁的json數(shù)據(jù),id為詳情頁鏈接所需標志

image.png
image.png

(5)詳情頁數(shù)據(jù)

抓取內(nèi)容:


image.png
image.png

字段:標題,作者,日期,簡要,標簽,內(nèi)容

查看源碼,數(shù)據(jù)全在var props所包含的script標簽里面

(6)正則獲取并將之轉為正常的json數(shù)據(jù)(理由:json文件可以更好的獲取某個字段的內(nèi)容,單純?nèi)谜齽t截取的話,不好獲取或者直接是獲取不到)

image.png

源碼

# -*- coding: utf-8 -*-
# @Time    : 2018/7/28 17:13
# @Author  : 蛇崽
# @Email   : 643435675@QQ.com 1532773314218
# @File    : 36kespider.py
import json

import re
import scrapy
import time

class ke36Spider(scrapy.Spider):

    name = 'ke36'

    allowed_domains = ['www.36kr.com']

    start_urls = ['https://36kr.com/']

    def parse(self, response):
        print('start parse -------------------------  ')
        word = '機器人'
        t = time.time()
        page = '1'
        print('t',t)
        for page in range(1,200):
            burl = 'http://36kr.com/api//search/entity-search?page={}&per_page=40&keyword={}&entity_type=post'.format(page,word)
            yield scrapy.Request(burl,callback=self.parse_list,dont_filter=True)

    def parse_list(self,response):
        res = response.body.decode('utf-8')
        # print(res)

        jdata = json.loads(res)
        code = jdata['code']
        timestamp = jdata['timestamp']
        timestamp_rt = jdata['timestamp_rt']
        items = jdata['data']['items']
        m_id =  items[0]['id']
        for item in items:
            m_id = item['id']
            b_url = 'http://36kr.com/p/{}.html'.format(str(m_id))
            # b_url = 'http://36kr.com/p/5137751.html'
            yield scrapy.Request(b_url,callback=self.parse_detail,dont_filter=True)

    def parse_detail(self,response):
        res = response.body.decode('utf-8')
        content = re.findall(r'<script>var props=(.*?)</script>',res)
        temstr = content[0]

        minfo = re.findall('\"detailArticle\|post\"\:(.*?)"hotPostsOf30',temstr)[0]
        print('minfo -----------------------------  ')
        minfo = minfo.rstrip(',')
        jdata = json.loads(minfo)
        print('j'*40)

        published_at = jdata['published_at']
        username = jdata['user']['name']
        title = jdata['user']['title']
        extraction_tags = jdata['extraction_tags']
        content = jdata['content']
        print(published_at,username,title,extraction_tags)
        print('*'*50)
        print(content)

更多資源請訪問:

https://blog.csdn.net/xudailong_blog/article/details/78762262

小密圈精品源碼根據(jù)地

歡迎光臨我的小網(wǎng)站:http://www.00reso.com

陸續(xù)優(yōu)化中,后續(xù)會開發(fā)更多更好玩的有趣的小工具

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 發(fā)詩了發(fā)詩了~~沉默的看官們,快來和我一起討論吧~ 一、 我打開門的時候 穿著明白藍色的那個女人 在愛情的天空 過...
    寒風如紙閱讀 358評論 4 1
  • 喜歡一首歌,因為歌里有我的故事;喜歡一座城,因為城里有你的足跡。人的一生不停的走著,只為發(fā)現(xiàn)更多精彩…… 我喜歡旅...
    Hooray_W閱讀 292評論 0 1
  • 差點就要錯過晚安筆記。真怕一覺睡到自然醒,還好家里有人晚睡,把我給“吵醒”了,才得以爬起來把筆記寫了。 回到家一天...
    茉莉大大閱讀 215評論 0 0
  • 可能是一個眼神就知道對方在想什么這就是閨蜜,無話不談也是閨蜜,相愛相殺的更是閨蜜,穿衣服反差很大的也是閨蜜,遠遠的...
    leestudio1980閱讀 244評論 0 0

友情鏈接更多精彩內(nèi)容