用python爬取i春秋的課程

來源:互聯網
上載者:User

標籤:diff   tps   擷取   url   lan   爬蟲   出現   print   res   

看課中內容是用get請求進行爬取課程,自己實踐的時候發現已經被改成post請求了,下面開始

開啟課程頁面

我用的Firefox,然後就是F12,點擊網路,可能會有很多包,但不影響,點擊刪除就行,然後點擊第二頁,其他頁面也行,可以看出總共是有17頁的

然後會出現很多包,找到下面這個,點擊查看

點擊後會看到訊息頭請求網址,這個就是我們要用的url

複製到代碼中

i_url = ‘https://www.ichunqiu.com/courses/ajaxCourses‘

接下來查看參數

然後發現“pageIndex”,很明顯是指的第幾頁,可以試一下第四頁數值是4,,,。這是post要用的表單資料,搞進代碼,因為pageIndex值需要改變,我將它設為空白值

post_data = {                ‘courseTag‘:‘‘,                ‘courseDiffcuty‘:‘‘,                ‘IsExp‘:‘‘,                ‘producerId‘:‘‘,                ‘orderField‘:‘‘,                ‘orderDirection‘:‘‘,                ‘pageIndex‘:‘‘,                ‘tagType‘:‘‘            }

由於i春秋設定了防爬蟲處理,因此我們需要添加headers,headers就不多說了(不想暴露自己的配置,我不會告訴你我還用的xp),用Firefox很簡單,點擊編輯重發,複製就行。接下來就是查看響應

很明顯這是json資料,先import json再說,可以找到課是在course中的result下,從0開始,我們可以用“len(raw_data[‘course‘][‘result‘])”查看該頁面有幾個課程,用raw_data[‘course‘][‘result‘][0][‘courseName’]來擷取第一個課程名字,下面是代碼

#-------------#Author:Glasses#-------------import requestsimport jsondef getData():        i_url = ‘https://www.ichunqiu.com/courses/ajaxCourses‘       headers = {                   }#headers 自己填    for p_index in range(1,18):        post_data = {                ‘courseTag‘:‘‘,                ‘courseDiffcuty‘:‘‘,                ‘IsExp‘:‘‘,                ‘producerId‘:‘‘,                ‘orderField‘:‘‘,                ‘orderDirection‘:‘‘,                ‘pageIndex‘:‘‘,                ‘tagType‘:‘‘            }        post_data[‘pageIndex‘] = p_index        r = requests.post(i_url,headers=headers,data=post_data,timeout=10)        raw_data = json.loads(r.text)                for i in range(len(raw_data[‘course‘][‘result‘])):            print raw_data[‘course‘][‘result‘][i][‘courseName‘]getData()    

來i春秋一起學習吧,

進階吧!小白,只要摔不死我就繼續摔!

用python爬取i春秋的課程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.