Python 抓取AnJuKe,以及json解析

來源:互聯網
上載者:User

標籤:json   close   ==   ret   log   json對象   chrome   lin   mac   

最近使用python 爬一些資料,anjuke的資料用了很久的時間,記錄下

由於目前技術有限,在chrome中都找不到ajax 的post或get 的資料,一度一籌莫展,後來無意中發現了它的手機端web站,發現可以使用。

python 代碼很簡單,

 1 import json 2  3 import requests 4  5 from lxml import etree 6  7 current_url = ‘http://m.anjuke.com/qd/rentlistbypage/?from=anjuke_home&page={page}‘ 8 headers = { 9     ‘User-Agent‘: ‘Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36‘}10 allinfo = []11 12 13 # 返回的是json14 def getdata(url):15     resouce = requests.get(url, headers=headers)16     resouce.encoding = ‘utf-8‘17     JsonResult = resouce.text18     return JsonResult19 20 21 if __name__ == ‘__main__‘:22     f = open(‘Conker.txt‘, ‘w‘)23     for i in range(1, 10):24         page = i25         OBJ = getdata(current_url.format(page=page))26         Resolve_data = json.loads(OBJ)27         DataResouce = Resolve_data["datas"]28         Info=DataResouce["list_info"]29         for cc in range(0,len(Info)):30 31 32 33             f.write(Info[cc]["title"]+";"+"圖片:"+Info[cc]["img"])34             f.write(‘\n‘)35 36 37     f.close()

這裡的問題是關於json解析的,網上找的問題,首先擷取到的資料是


json對象,如果直接使用 會報錯,

這裡要使用json.load() 來載入以後才可以使用

Python 抓取AnJuKe,以及json解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.