有些網站的數據是通過ajax請求獲取的,或者提供了json格式的api。
比如對於如下的數據:
- {
- {
- "url": "http://www.techbrood.com/news/1",
- "author": "iefreer",
- "title": "techbrood Co. test 1"
- },
- {
- "url": "http://www.techbrood.com/news/2",
- "author": "ryan.chen",
- "title": "techbrood Co. test 2"
- }
- }
在Scrapy裏,只要簡單改寫下parse函數就行:
- def parse(self, response):
- sites = json.loads(response.body_as_unicode())
- for site in sites:
- print site['url']
調用body_as_unicode()是爲了能處理unicode編碼的數據。