Scrapy解析iframe网址

时间:2017-05-06 10:21:09

标签: python-2.7 iframe scrapy centos6.5

我正在解析网站上的链接,然后尝试解析iframe src的链接。

  • 根据DEBUG看起来第一个链接正在被正确解析,但我的输出文件中没有任何数据。

  • 是否也可以删除后的所有内容?在URL中。这个 看起来像是嵌入的iframe信息。

我正在运行Centos 6.5 Python 2.7.5

scrapy runspider new.py -o videos.csv

import scrapy

class PdgaSpider(scrapy.Spider):
    name = "pdgavideos"
    start_urls = ["http://www.pdga.com/videos/"]

    def parse(self, response):
        for link in response.xpath('//td[2]/a/@href').extract():

            from scrapy.http.request import Request
            yield Request('http://www.pdga.com'+link, callback=self.parse_page, meta={'link':link})

    def parse_page(self, response):
        for frame in response.xpath("//player").extract():

            yield {
                'link': response.urljoin(frame)
            }

调试结果

DEBUG: Crawled (200) <GET http://www.pdga.com/videos/2017-gbo-final-round-front-9-sexton-mcbeth-mccray-newhouse> (referer: http://www.pdga.com/videos/)
DEBUG: Crawled (200) <GET http://www.pdga.com/videos/2017-glass-blown-open-fpo-rd-2-pt-1-pierce-fajkus-leatherman-c-allen-sexton-leatherman> (referer: http://www.pdga.com/videos/)
DEBUG: Crawled (200) <GET http://www.pdga.com/videos/2017-gbo-final-round-back-9-sexton-mcbeth-mccray-newhouse> (referer: http://www.pdga.com/videos/)

预期结果

http://www.youtube.com/embed/tYBF-BaqVJ8

1 个答案:

答案 0 :(得分:1)

Scrapy doese没有刮掉iFrames的内容,但你可以得到它们。首先获取iframe网址,然后在其上调用解析。

urls = response.css('iframe::attr(src)').extract()
for url in urls :
        yield scrapy.Request(url....)