首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单独使用scrapy可以抓取iframe的内容吗?

单独使用scrapy可以抓取iframe的内容吗?
EN

Stack Overflow用户
提问于 2014-06-19 15:47:37
回答 2查看 8.4K关注 0票数 3

我尝试过复制和粘贴站点的元素(Xpath),但没有返回任何结果。

抓取可以抓取iframe中的数据吗?如果是,如何做,如果不是,还应该做些什么?谢谢!

代码语言:javascript
运行
复制
rules = (Rule (SgmlLinkExtractor(deny = path_deny_base, restrict_xpaths=('*'))
    , callback="parse", follow= True),
    )


    def parse(self, response):
        yield(Request(url, callback = self.parse_iframe))

    def parse_iframe(self, response):
        #your code to scrape the content from iframe
        #def parse_items(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.select('//div[2]/h1')
            #//div[2]/h1
        linker = hxs.select('//div[2]/div[10]/a[1]')
            #//div[2]/div[10]/a[1]
        loc_Con = hxs.select('//div[2]/div[1]/div[2]/span/span/span[1]') #//div[2]/div[1]/div[2]/span/span/span[1]
        loc_Reg = hxs.select('//div[2]/div[1]/div[2]/span/span/span[2]') #/div[2]/div[1]/div[2]/span/span/span[2]
        loc_Loc = hxs.select('//div[2]/div[1]/div[2]/span/span/span[3]') #/div[2]/div[1]/div[2]/span/span/span[3]
        items = []
        for titles in titles:
            item = CraigslistSampleItem()
            #item ["job_id"] = id.select('text()').extract()[0].strip()
            item ["title"] = map(unicode.strip, titles.select('text()').extract()) #ok
            item ["link"] = linker.select('@href').extract() #ok
            item ["info"] = (response.url)
            temp1 = loc_Con.select('text()').extract()
            temp2 = loc_Reg.select('text()').extract()
            temp3 = loc_Loc.select('text()').extract()
            temp1 = temp1[0] if temp1 else ""
            temp2 = temp2[0] if temp2 else ""
            temp3 = temp3[0] if temp3 else ""
            item["code"] = "{0}-{1}-{2}".format(temp1, temp2, temp3)
            items.append(item)
        return(items)
EN

回答 2

Stack Overflow用户

发布于 2014-06-19 16:36:38

Scrapy无法从iframe中抓取内容。而是向iframe URL发出请求,如下所示:

代码语言:javascript
运行
复制
def parse(self, response):
    yield(Request(url, callback = self.parse_iframe))

def parse_iframe(self, response):
    #your code to scrape the content from iframe

其中,url应为iframe url,例如https://career-meridia....../jobs)

编辑:

将url替换为带红色下划线的部分。

Edit2:确保您已经传递了iframe url所需的每个参数。否则你什么也得不到。如果是post方法,你必须传递所有的post参数。

票数 5
EN

Stack Overflow用户

发布于 2017-07-21 19:08:34

这就是我正在做的事情。首先获取iframe url,然后对其再次调用parse。

代码语言:javascript
运行
复制
urls = response.css('iframe::attr(src)').extract()
for url in urls :
        yield scrapy.Request(url....)
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24301376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档