用Python獲取亞馬遜商品信息
引言
亞馬遜網(wǎng)站相較于國(guó)內(nèi)的購(gòu)物網(wǎng)站,可以直接使用python的最基本的requests進(jìn)行請(qǐng)求。訪問(wèn)不是過(guò)于頻繁,在未觸發(fā)保護(hù)機(jī)制的情況下,可以獲取我們想要的數(shù)據(jù)。本次通過(guò)以下三部分簡(jiǎn)單介紹下基本爬取流程:
使用requests的get請(qǐng)求,獲取亞馬遜列表和詳情頁(yè)的頁(yè)面內(nèi)容使用css/xpath對(duì)獲取的內(nèi)容進(jìn)行解析,取得關(guān)鍵數(shù)據(jù)動(dòng)態(tài)IP的作用及其使用方法
一、獲取亞馬遜列表頁(yè)的信息
以游戲區(qū)為例:

獲取列表內(nèi)能獲取到的商品信息,如商品名,詳情鏈接,進(jìn)一步獲取其他內(nèi)容。
用requests.get()獲取網(wǎng)頁(yè)內(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)此時(shí)已經(jīng)獲取的當(dāng)前列表頁(yè)目前能獲得的信息:

二、獲取詳情頁(yè)信息
進(jìn)入詳情頁(yè):

進(jìn)入詳情頁(yè)之后,能獲得更多的內(nèi)容
用requests.get()獲取網(wǎng)頁(yè)內(nèi)容,css選取相關(guān)標(biāo)簽的內(nèi)容:
res = requests.get(itemUrl, headers=headers)
content = res.content.decode('utf-8')
Select = Selector(text=content)
itemPic = Select.css('#main-image::attr(src)').extract_first()
itemPrice = Select.css('.a-offscreen::text').extract_first()
itemInfo = Select.css('#feature-bullets').extract_first()
data = {}
data['itemUrl'] = itemUrl
data['itemName'] = itemName
data['itemPic'] = itemPic
data['itemPrice'] = itemPrice
data['itemInfo'] = itemInfo
print(data)此時(shí)已經(jīng)生成詳情頁(yè)數(shù)據(jù)的信息:

目前涉及到的就是最基本的requests請(qǐng)求亞馬遜并用css/xpath獲取相應(yīng)的信息。
三、代理設(shè)置
目前,國(guó)內(nèi)訪問(wèn)亞馬遜會(huì)很不穩(wěn)定,我這邊大概率會(huì)出現(xiàn)連接不上的情況。如果真的需要去爬取亞馬遜的信息,最好使用一些穩(wěn)定的代理,我這邊自己使用的是ipidea的代理,可以白嫖50M流量。如果有代理的話訪問(wèn)的成功率會(huì)高,速度也會(huì)快一點(diǎn)。
代理使用有兩種方式,一是通過(guò)api獲取IP地址,還有用賬密的方式使用,方法如下:
3.1.1 api獲取代理


3.1.2 api獲取ip代碼
def getProxies():
# 獲取且僅獲取一個(gè)ip
api_url = '生成的api鏈接'
res = requests.get(api_url, timeout=5)
try:
if res.status_code == 200:
api_data = res.json()['data'][0]
proxies = {
'http': 'http://{}:{}'.format(api_data['ip'], api_data['port']),
'https': 'http://{}:{}'.format(api_data['ip'], api_data['port']),
}
print(proxies)
return proxies
else:
print('獲取失敗')
except:
print('獲取失敗')3.2.1 賬密獲取代理
因?yàn)槭琴~密驗(yàn)證,所以需要 去到賬戶中心填寫信息創(chuàng)建子賬戶:


創(chuàng)建好子賬戶之后,根據(jù)賬號(hào)和密碼獲取鏈接:
3.2.2 賬密獲取代理代碼
# 獲取賬密ip
def getAccountIp():
# 測(cè)試完成后返回代理proxy
mainUrl = 'https://api.myip.la/en?json'
headers = {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"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",
}
entry = 'http://{}-zone-custom{}:proxy.ipidea.io:2334'.format("帳號(hào)", "密碼")
proxy = {
'http': entry,
'https': entry,
}
try:
res = requests.get(mainUrl, headers=headers, proxies=proxy, timeout=10)
if res.status_code == 200:
return proxy
except Exception as e:
print("訪問(wèn)失敗", e)
pass使用代理之后,亞馬遜商品信息的獲取改善了不少,之前代碼會(huì)報(bào)各種連接失敗的錯(cuò)誤,在requests請(qǐng)求之前調(diào)用代理獲取的方法,方法return回代理ip并加入requests請(qǐng)求參數(shù),就可以實(shí)現(xiàn)代理請(qǐng)求了。
四、全部代碼
# coding=utf-8
import requests
from parsel import Selector
from urllib.parse import urljoin
def getProxies():
# 獲取且僅獲取一個(gè)ip
api_url = '生成的api鏈接'
res = requests.get(api_url, timeout=5)
try:
if res.status_code == 200:
api_data = res.json()['data'][0]
proxies = {
'http': 'http://{}:{}'.format(api_data['ip'], api_data['port']),
'https': 'http://{}:{}'.format(api_data['ip'], api_data['port']),
}
print(proxies)
return proxies
else:
print('獲取失敗')
except:
print('獲取失敗')
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",
}
proxies = getProxies()
resp = requests.get(spiderurl, headers=headers, proxies=proxies)
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)
proxies = getProxies()
res = requests.get(itemUrl, headers=headers, proxies=proxies)
content = res.content.decode('utf-8')
Select = Selector(text=content)
itemPic = Select.css('#main-image::attr(src)').extract_first()
itemPrice = Select.css('.a-offscreen::text').extract_first()
itemInfo = Select.css('#feature-bullets').extract_first()
data = {}
data['itemUrl'] = itemUrl
data['itemName'] = itemName
data['itemPic'] = itemPic
data['itemPrice'] = itemPrice
data['itemInfo'] = itemInfo
print(data)通過(guò)上面的步驟,可以實(shí)現(xiàn)最基礎(chǔ)的亞馬遜的信息獲取。
目前只獲得最基本的數(shù)據(jù),若想獲得更多也可以自行修改xpath/css選擇器去拿到你想要的內(nèi)容。而且穩(wěn)定的動(dòng)態(tài)IP能是你進(jìn)行請(qǐng)求的時(shí)候少一點(diǎn)等待的時(shí)間,無(wú)論是編寫中的測(cè)試還是小批量的爬取,都能提升工作的效率。以上就是全部的內(nèi)容。
總結(jié)
到此這篇關(guān)于用Python獲取亞馬遜商品信息的文章就介紹到這了,更多相關(guān)Python亞馬遜商品信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
apache部署python程序出現(xiàn)503錯(cuò)誤的解決方法
這篇文章主要給大家介紹了關(guān)于在apahce部署python程序出現(xiàn)503錯(cuò)誤的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)同樣遇到這個(gè)問(wèn)題的朋友們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07
利用python實(shí)現(xiàn)平穩(wěn)時(shí)間序列的建模方式
這篇文章主要介紹了利用python實(shí)現(xiàn)平穩(wěn)時(shí)間序列的建模方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python中sys.argv函數(shù)精簡(jiǎn)概括
本篇文章給大家分享了關(guān)于python中sys.argv函數(shù)的相關(guān)知識(shí)點(diǎn),有興趣的朋友可以參考學(xué)習(xí)下。2018-07-07
Python 通過(guò)正則表達(dá)式快速獲取電影的下載地址
這篇文章主要介紹了Python 通過(guò)正則表達(dá)式快速獲取電影的下載地址,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
python性能測(cè)試對(duì)手機(jī)號(hào)綁定進(jìn)行壓測(cè)
這篇文章主要為大家介紹了python性能測(cè)試對(duì)手機(jī)號(hào)綁定進(jìn)行壓測(cè)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
使用pickle存儲(chǔ)數(shù)據(jù)dump 和 load實(shí)例講解
今天小編就為大家分享一篇使用pickle存儲(chǔ)數(shù)據(jù)dump 和 load實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python 實(shí)現(xiàn)自動(dòng)化Excel報(bào)表的步驟
這篇文章主要介紹了Python 實(shí)現(xiàn)自動(dòng)化Excel報(bào)表的步驟,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04
python 逆向爬蟲(chóng)正確調(diào)用 JAR 加密邏輯
這篇文章主要介紹了python 逆向爬蟲(chóng)正確調(diào)用 JAR 加密邏輯,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01

