引言
亞馬遜網(wǎng)站相較于國內(nèi)的購物網(wǎng)站,可以直接使用python的最基本的requests進(jìn)行請求。訪問不是過于頻繁,在未觸發(fā)保護(hù)機(jī)制的情況下,可以獲取我們想要的數(shù)據(jù)。本次通過以下三部分簡單介紹下基本爬取流程:
-
使用requests的get請求,獲取亞馬遜列表和詳情頁的頁面內(nèi)容
-
使用css/xpath對獲取的內(nèi)容進(jìn)行解析,取得關(guān)鍵數(shù)據(jù)
-
動態(tài)IP的作用及其使用方法
一、獲取亞馬遜列表頁的信息
以游戲區(qū)為例:
程序員必備接口測試調(diào)試工具:立即使用
Apipost = Postman + Swagger + Mock + Jmeter
Api設(shè)計(jì)、調(diào)試、文檔、自動化測試工具
后端、前端、測試,同時(shí)在線協(xié)作,內(nèi)容實(shí)時(shí)同步
獲取列表內(nèi)能獲取到的商品信息,如商品名,詳情鏈接,進(jìn)一步獲取其他內(nèi)容。
用requests.get()獲取網(wǎng)頁內(nèi)容,設(shè)置好header,利用xpath選擇器選取相關(guān)標(biāo)簽的內(nèi)容:
import requests from parsel import Selector from urllib.parse import urljoin spiderurl = 'https://www.amazon.com/s?i=videogames-intl-ship' headers = { "authority": "www.amazon.com", "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Mobile/14G60 MicroMessenger/6.5.19 NetType/4G Language/zh_TW", } resp = requests.get(spiderurl, headers=headers) content = resp.content.decode('utf-8') select = Selector(text=content) nodes = select.xpath("http://a[@title='product-detail']") for node in nodes: itemUrl = node.xpath("./@href").extract_first() itemName = node.xpath("./div/h2/span/text()").extract_first() if itemUrl and itemName: itemUrl = urljoin(spiderurl,itemUrl)#用urljoin方法湊完整鏈接 print(itemUrl,itemName)
登錄后復(fù)制
此時(shí)已經(jīng)獲取的當(dāng)前列表頁目前能獲得的信息:
二、獲取詳情頁信息
進(jìn)入詳情頁:
進(jìn)入詳情頁之后,能獲得