用Python獲取亞馬遜商品信息
引言
亞馬遜網(wǎng)站相較于國內(nèi)的購物網(wǎng)站,可以直接使用python的最基本的requests進行請求。訪問不是過于頻繁,在未觸發(fā)保護機制的情況下,可以獲取我們想要的數(shù)據(jù)。本次通過以下三部分簡單介紹下基本爬取流程:
使用requests的get請求,獲取亞馬遜列表和詳情頁的頁面內(nèi)容使用css/xpath對獲取的內(nèi)容進行解析,取得關(guān)鍵數(shù)據(jù)動態(tài)IP的作用及其使用方法
一、獲取亞馬遜列表頁的信息
以游戲區(qū)為例:
獲取列表內(nèi)能獲取到的商品信息,如商品名,詳情鏈接,進一步獲取其他內(nèi)容。
用requests.get()獲取網(wǎng)頁內(nèi)容,設(shè)置好header,利用xpath選擇器選取相關(guān)標簽的內(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)
此時已經(jīng)獲取的當(dāng)前列表頁目前能獲得的信息:
二、獲取詳情頁信息
進入詳情頁:
進入詳情頁之后,能獲得更多的內(nèi)容
用requests.get()獲取網(wǎng)頁內(nèi)容,css選取相關(guān)標簽的內(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)
此時已經(jīng)生成詳情頁數(shù)據(jù)的信息:
目前涉及到的就是最基本的requests請求亞馬遜并用css/xpath獲取相應(yīng)的信息。
三、代理設(shè)置
目前,國內(nèi)訪問亞馬遜會很不穩(wěn)定,我這邊大概率會出現(xiàn)連接不上的情況。如果真的需要去爬取亞馬遜的信息,最好使用一些穩(wěn)定的代理,我這邊自己使用的是ipidea的代理,可以白嫖50M流量。如果有代理的話訪問的成功率會高,速度也會快一點。
代理使用有兩種方式,一是通過api獲取IP地址,還有用賬密的方式使用,方法如下:
3.1.1 api獲取代理
3.1.2 api獲取ip代碼
def getProxies(): # 獲取且僅獲取一個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 賬密獲取代理
因為是賬密驗證,所以需要 去到賬戶中心填寫信息創(chuàng)建子賬戶:
創(chuàng)建好子賬戶之后,根據(jù)賬號和密碼獲取鏈接:
3.2.2 賬密獲取代理代碼
# 獲取賬密ip def getAccountIp(): # 測試完成后返回代理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("帳號", "密碼") 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
使用代理之后,亞馬遜商品信息的獲取改善了不少,之前代碼會報各種連接失敗的錯誤,在requests請求之前調(diào)用代理獲取的方法,方法return回代理ip并加入requests請求參數(shù),就可以實現(xiàn)代理請求了。
四、全部代碼
# coding=utf-8 import requests from parsel import Selector from urllib.parse import urljoin def getProxies(): # 獲取且僅獲取一個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)
通過上面的步驟,可以實現(xiàn)最基礎(chǔ)的亞馬遜的信息獲取。
目前只獲得最基本的數(shù)據(jù),若想獲得更多也可以自行修改xpath/css選擇器去拿到你想要的內(nèi)容。而且穩(wěn)定的動態(tài)IP能是你進行請求的時候少一點等待的時間,無論是編寫中的測試還是小批量的爬取,都能提升工作的效率。以上就是全部的內(nèi)容。
總結(jié)
到此這篇關(guān)于用Python獲取亞馬遜商品信息的文章就介紹到這了,更多相關(guān)Python亞馬遜商品信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python pyinotify模塊實現(xiàn)對文檔的實時監(jiān)控功能方法
今天小編就為大家分享一篇Python pyinotify模塊實現(xiàn)對文檔的實時監(jiān)控功能方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10Python實現(xiàn)GUI學(xué)生管理系統(tǒng)的示例代碼
這篇文章主要為大家介紹了如何留Python語言實現(xiàn)簡易的GUI學(xué)生管理系統(tǒng),文中的示例代碼講解詳細,對我們學(xué)習(xí)Python有一定幫助,需要的可以參考下2022-06-06Python基于sftp及rsa密匙實現(xiàn)遠程拷貝文件的方法
這篇文章主要介紹了Python基于sftp及rsa密匙實現(xiàn)遠程拷貝文件的方法,結(jié)合實例形式分析了基于RSA秘鑰遠程登陸及文件操作的相關(guān)技巧,需要的朋友可以參考下2016-09-09使用Python程序抓取新浪在國內(nèi)的所有IP的教程
這篇文章主要介紹了使用Python程序抓取新浪在國內(nèi)的所有IP的教程,作為Python網(wǎng)絡(luò)編程中獲取IP的一個小實踐,需要的朋友可以參考下2015-05-05python打包為linux可執(zhí)行文件的詳細圖文教程
這篇文章主要給大家介紹了關(guān)于python打包為linux可執(zhí)行文件的詳細圖文教程,本文介紹的方法可以輕松地將Python代碼變成獨立的可執(zhí)行文件,需要的朋友可以參考下2024-02-02