python - 使用Scrapy爬取本地XML文件-起始URL 本地文件地址

标签 python xml xpath scrapy scrapy-spider

我想用scrapy抓取我在我的下载文件夹中的一个本地xml文件,使用xpath提取相关信息。

将 scrapy 简介用作 guide

2016-01-24 12:38:53 [scrapy] DEBUG: Retrying <GET file://home/sayth/Downloads/20160123RAND0.xml> (failed 2 times): [Errno 2] No such file or directory: '/sayth/Downloads/20160123RAND0.xml'
2016-01-24 12:38:53 [scrapy] DEBUG: Gave up retrying <GET file://home/sayth/Downloads/20160123RAND0.xml> (failed 3 times): [Errno 2] No such file or directory: '/sayth/Downloads/20160123RAND0.xml'
2016-01-24 12:38:53 [scrapy] ERROR: Error downloading <GET file://home/sayth/Downloads/20160123RAND0.xml>

我已经尝试了以下几个版本,但是我无法获得开始 url 来接受我的文件。

# -*- coding: utf-8 -*-
import scrapy


class MyxmlSpider(scrapy.Spider):
    name = "myxml"
    allowed_domains = ["file://home/sayth/Downloads"]
    start_urls = (
        'http://www.file://home/sayth/Downloads/20160123RAND0.xml',
    )

    def parse(self, response):
        for file in response.xpath('//meeting'):
            full_url = response.urljoin(href.extract())
            yield scrapy.Request(full_url, callback=self.parse_question)

    def parse_xml(self, response):
        yield {
            'name': response.xpath('//meeting/race').extract()
        }

只是为了确认我确实在那个位置有文件

sayth@sayth-HP-EliteBook-2560p : ~/Downloads
[0] % ls -a
.                                                              Building a Responsive Website with Bootstrap [Video].zip
..                                                             codemirror.zip
1.1 Situation Of Long Term Gain.xls                            Complete-Python-Bootcamp-master.zip
2008 Racedata.xls                                              Cox Plate 2005.xls
20160123RAND0.xml  

最佳答案

根本不要指定 allowed_domains 并在协议(protocol)后使用 3 个斜杠:

start_urls = ["file:///home/sayth/Downloads/20160123RAND0.xml"]

关于python - 使用Scrapy爬取本地XML文件-起始URL 本地文件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34971287/

相关文章:

java - 单击表数据 selenium java 的元素时出现问题

python - 是否可以获取用于评估 xpath 结果的所有上下文节点?

python - 加速使用前一行结果的行操作

javascript - XMLHttpRequest 从本地服务器而不是外部服务器加载 XML?

python - 使用 sqlalchemy 进行 bool 字段查询

xml - 骡子 http :outbound-endpoint + multipart/form-data

python - 使用 python 时 xml 中缺少属性

xml - 为什么这个 XPath 表达式在 xmlstarlet 中没有返回正确的值?

python - Scipy io 读取 wavfile 错误

python - 如何使 float 在 python 3 中具有特定范围(或者我可以)?