亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

用Python獲取亞馬遜商品信息

 更新時(shí)間:2022年01月19日 08:45:33   作者:CorGi_8456  
大家好,本篇文章主要講的是用Python獲取亞馬遜商品信息,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽

引言

        亞馬遜網(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)證,所以需要 去到賬戶中心填寫(xiě)信息創(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ú)論是編寫(xiě)中的測(cè)試還是小批量的爬取,都能提升工作的效率。以上就是全部的內(nèi)容。 

總結(jié)

到此這篇關(guān)于用Python獲取亞馬遜商品信息的文章就介紹到這了,更多相關(guān)Python亞馬遜商品信息內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論