您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“怎么用Python獲取Amazon亞馬遜的商品信息”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
以游戲區(qū)為例:
獲取列表內(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("//a[@title='product-detail']") for node in nodes: itemUrl = node.xpath("./@href").extract_first() itemName = node.xpath("./div/h3/span/text()").extract_first() if itemUrl and itemName: itemUrl = urljoin(spiderurl,itemUrl)#用urljoin方法湊完整鏈接 print(itemUrl,itemName)
此時(shí)已經(jīng)獲取的當(dāng)前列表頁目前能獲得的信息:
進(jìn)入詳情頁:
進(jìn)入詳情頁之后,能獲得更多的內(nèi)容
用requests.get()獲取網(wǎng)頁內(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)生成詳情頁數(shù)據(jù)的信息:
目前涉及到的就是最基本的requests請(qǐng)求亞馬遜并用css/xpath獲取相應(yīng)的信息。
目前,國(guó)內(nèi)訪問亞馬遜會(huì)很不穩(wěn)定,我這邊大概率會(huì)出現(xiàn)連接不上的情況。如果真的需要去爬取亞馬遜的信息,最好使用一些穩(wěn)定的代理,我這邊自己使用的是ipidea的代理,可以白嫖500M流量。如果有代理的話訪問的成功率會(huì)高,速度也會(huì)快一點(diǎn)。
網(wǎng)址在這里:http://www.ipidea.net/?utm-source=PHP&utm-keyword=?PHP
代理使用有兩種方式,一是通過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 賬密獲取代理 (注冊(cè)地址:http://www.ipidea.net/?utm-source=PHP&utm-keyword=?PHP )
因?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("訪問失敗", 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("//a[@title='product-detail']") for node in nodes: itemUrl = node.xpath("./@href").extract_first() itemName = node.xpath("./div/h3/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)
“怎么用Python獲取Amazon亞馬遜的商品信息”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。