用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)證,所以需要 去到賬戶中心填寫(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)文章
Python pyinotify模塊實(shí)現(xiàn)對(duì)文檔的實(shí)時(shí)監(jiān)控功能方法
今天小編就為大家分享一篇Python pyinotify模塊實(shí)現(xiàn)對(duì)文檔的實(shí)時(shí)監(jiān)控功能方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python實(shí)現(xiàn)GUI學(xué)生管理系統(tǒng)的示例代碼
這篇文章主要為大家介紹了如何留Python語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易的GUI學(xué)生管理系統(tǒng),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考下2022-06-06Python基于sftp及rsa密匙實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法
這篇文章主要介紹了Python基于sftp及rsa密匙實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法,結(jié)合實(shí)例形式分析了基于RSA秘鑰遠(yuǎn)程登陸及文件操作的相關(guān)技巧,需要的朋友可以參考下2016-09-09使用Python程序抓取新浪在國(guó)內(nèi)的所有IP的教程
這篇文章主要介紹了使用Python程序抓取新浪在國(guó)內(nèi)的所有IP的教程,作為Python網(wǎng)絡(luò)編程中獲取IP的一個(gè)小實(shí)踐,需要的朋友可以參考下2015-05-05python打包為linux可執(zhí)行文件的詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于python打包為linux可執(zhí)行文件的詳細(xì)圖文教程,本文介紹的方法可以輕松地將Python代碼變成獨(dú)立的可執(zhí)行文件,需要的朋友可以參考下2024-02-02淺談pyqt5在QMainWindow中布局的問(wèn)題
今天小編就為大家分享一篇淺談pyqt5在QMainWindow中布局的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06