【问题标题】:Scrapy, crawl data by onclickScrapy,通过onclick抓取数据
【发布时间】:2019-06-29 08:35:44
【问题描述】:

我想在这个链接中提取每篇论文的标题和pdf链接:https://iclr.cc/Conferences/2019/Schedule?type=Poster

我的代码在这里

class ICLRCrawler(Spider):
    name = "ICLRCrawler"
    allowed_domains = ["iclr.cc"]
    start_urls = ["https://iclr.cc/Conferences/2019/Schedule?type=Poster", ]

    def parse(self, response):
        papers = Selector(response).xpath('//*[@id="content"]/div/div[@class="paper"]')
        titles = Selector(response).xpath('//*[@id="maincard_704"]/div[3]')
        links = Selector(response).xpath('//*[@id="maincard_704"]/div[6]/a[2]')
        for title, link in zip(titles, links):
            item = PapercrawlerItem()
            item['title'] = title.xpath('text()').extract()[0]
            item['pdf'] = link.xpath('/@href').extract()[0]
            item['sup'] = ''
            yield item 

但是,要获得每篇论文的标题和链接似乎并不容易。在这里,如何更改代码以获取数据?

【问题讨论】:

    标签: python web-scraping scrapy


    【解决方案1】:

    您可以使用更简单的方法:

    def parse(self, response):
    
        for poster in response.xpath('//div[starts-with(@id, "maincard_")]'):
            item = PapercrawlerItem()
            item["title"] = poster.xpath('.//div[@class="maincardBody"]/text()[1]').get()
            item["pdf"] = poster.xpath('.//a[@title="PDF"]/@href').get()
    
            yield item
    

    【讨论】:

    • 你的回答真的很棒。要知道我应该提取什么xpath很难,我怎样才能更好地理解它?
    • @GoingMyWay 您使用的是古代风格代码 (Selector)。你在哪里得到它?我建议你阅读 Scrapy 文档,其中包含易于使用的示例。
    • 嗨,我4年前学过Scarpy,代码是我的旧代码。谢谢,我会阅读 Scrapy 的文档。
    【解决方案2】:

    您必须将Extract()[0] 替换为get_attribute('href')

    【讨论】:

    • links 为空。所以我认为不是这个问题。
    • 这是用于提取链接:-driver.find_element_by_xpath('//*[@id="maincard_704"]/div[6]/a[2]').get_attribute('href' ) 这是用于提取标题名称:-driver.find_element_by_xpath('//*[@id="maincard_676"]/div[3]').text
    猜你喜欢
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2017-09-04
    • 1970-01-01
    • 2022-08-04
    • 2022-01-18
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多