相关疑难解决方法(0)

硒与scrapy动态页面

我正在尝试使用scrapy从网页上抓取产品信息.我的待删节网页如下所示:

  • 从包含10个产品的product_list页面开始
  • 点击"下一步"按钮加载下10个产品(两个页面之间的网址不变)
  • 我使用LinkExtractor跟踪每个产品链接到产品页面,并获得我需要的所有信息

我试图复制next-button-ajax-call但是无法正常工作,所以我试试了selenium.我可以在一个单独的脚本中运行selenium的webdriver,但我不知道如何与scrapy集成.我应该把硒部分放在我的scrapy蜘蛛里?

我的蜘蛛非常标准,如下所示:

class ProductSpider(CrawlSpider):
    name = "product_spider"
    allowed_domains = ['example.com']
    start_urls = ['http://example.com/shanghai']
    rules = [
        Rule(SgmlLinkExtractor(restrict_xpaths='//div[@id="productList"]//dl[@class="t2"]//dt'), callback='parse_product'),
        ]

    def parse_product(self, response):
        self.log("parsing product %s" %response.url, level=INFO)
        hxs = HtmlXPathSelector(response)
        # actual data follows
Run Code Online (Sandbox Code Playgroud)

任何想法都表示赞赏.谢谢!

python selenium scrapy web-scraping selenium-webdriver

74
推荐指数
1
解决办法
7万
查看次数

单击Scrapy中的按钮

我正在使用Scrapy抓取网页.当您点击某个按钮时,我只需弹出一些我需要的信息(当然,点击后也会出现在HTML代码中).

我发现Scrapy可以处理的形式(如登录)如图所示这里.但问题是没有形式可以填写,所以这不是我需要的.

如何只需单击一个按钮,然后显示我需要的信息?

我是否必须使用像mechanize或lxml这样的外部库?

python web-crawler scrapy web-scraping

37
推荐指数
2
解决办法
4万
查看次数

用Scrapy和Selenium刮痧

我有一个scrapy蜘蛛爬行网站,通过页面上的JavaScript重新加载内容.为了进入下一页抓取,我一直在使用Selenium点击网站顶部的月份链接.

问题在于,即使我的代码按预期移动每个链接,蜘蛛也会抓住月份的第一个月(Sept)数据并返回此重复数据.

我怎么能绕过这个?

from selenium import webdriver

class GigsInScotlandMain(InitSpider):
        name = 'gigsinscotlandmain'
        allowed_domains = ["gigsinscotland.com"]
        start_urls = ["http://www.gigsinscotland.com"]


    def __init__(self):
        InitSpider.__init__(self)
        self.br = webdriver.Firefox()

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        self.br.get(response.url)
        time.sleep(2.5)
        # Get the string for each month on the page.
        months = hxs.select("//ul[@id='gigsMonths']/li/a/text()").extract()

        for month in months:
            link = self.br.find_element_by_link_text(month)
            link.click()
            time.sleep(5)

            # Get all the divs containing info to be scraped.
            listitems = hxs.select("//div[@class='listItem']")
            for listitem in listitems:
                item = GigsInScotlandMainItem()
                item['artist'] = listitem.select("div[contains(@class, 'artistBlock')]/div[@class='artistdiv']/span[@class='artistname']/a/text()").extract()
                #
                # …
Run Code Online (Sandbox Code Playgroud)

python selenium scrapy

6
推荐指数
1
解决办法
6002
查看次数