亚马逊网络抓取

时间:2015-03-31 21:59:10

标签: python web-scraping beautifulsoup phantomjs amazon

我试图用phantomjs和python来削减亚马逊的价格。我想用漂亮的汤来解析它,以获得书籍的新旧价格,问题是:当我通过使用phantomjs的请求来源时价格仅为0,00,代码就是这个简单的测试。

我是网络抓取新手,但我不明白亚马逊是否有措施避免价格上涨或我做错了因为我正在尝试其他更简单的网页而我可以得到我想要的数据。

PD我在一个不支持使用亚马逊API的国家/地区,这就是为什么刮刀是必要的

import re
import urlparse

from selenium import webdriver
from bs4 import BeautifulSoup
from time import sleep

link = 'http://www.amazon.com/gp/offer-listing/1119998956/ref=dp_olp_new?ie=UTF8&condition=new'#'http://www.amazon.com/gp/product/1119998956'

class AmzonScraper(object):
    def __init__(self):
        self.driver = webdriver.PhantomJS()
        self.driver.set_window_size(1120, 550)

    def scrape_prices(self):
        self.driver.get(link)
        s = BeautifulSoup(self.driver.page_source)
        return s

    def scrape(self):
        source = self.scrape_prices()
        print source
        self.driver.quit()

if __name__ == '__main__':
    scraper = TaleoJobScraper()
    scraper.scrape()

3 个答案:

答案 0 :(得分:3)

首先,要关注@Nick Bailey的评论,研究使用条款并确保您没有违规行为。

要解决此问题,您需要调整PhantomJS所需的功能

caps = webdriver.DesiredCapabilities.PHANTOMJS
caps["phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 (KHTML, like Gecko) Chrome/15.0.87"

self.driver = webdriver.PhantomJS(desired_capabilities=caps)
self.driver.maximize_window()

而且,要使其防弹,您可以Custom Expected Condition等待价格变为非零

from selenium.common.exceptions import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class wait_for_price(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try :
            element_text = EC._find_element(driver, self.locator).text.strip()
            return element_text != "0,00"
        except StaleElementReferenceException:
            return False

用法:

def scrape_prices(self):
    self.driver.get(link)

    WebDriverWait(self.driver, 200).until(wait_for_price((By.CLASS_NAME, "olpOfferPrice")))
    s = BeautifulSoup(self.driver.page_source)

    return s

答案 1 :(得分:2)

将phantomjs的用户代理设置为普通浏览器的用户代理的答案很好。既然你说你的国家被亚马逊封锁了,那么我想你也需要设置一个代理。

这是一个如何使用firefox useragent和代理在python中启动phantomJS的示例。

from selenium.webdriver import *
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
service_args = [ '--proxy=1.1.1.1:port', '--proxy-auth=username:pass'  ]
dcap = dict( DesiredCapabilities.PHANTOMJS )
dcap["phantomjs.page.settings.userAgent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0"
driver = PhantomJS( desired_capabilities = dcap, service_args=service_args )

其中1.1.1.1是您的代理IP和端口是代理端口。只有当您的代理需要身份验证时,才需要用户名和密码。

答案 2 :(得分:0)

另一个可以尝试的框架是Scrapy,它比用于模拟浏览器交互的硒简单。 Scrapy为您提供了使用CSS selectorsXPath轻松解析数据的类,以及一个以所需格式存储数据的管道,例如,将其写入MongoDB数据库< / p>

通常您可以编写一个完整构建的蜘蛛,并以 10行代码将其部署到Scrapy云中

观看此YT视频,了解如何将Scrapy用于scraping Amazon reviews作为用例