Python 需要使用scrapy AUTH解析XML文件
我的问题是,当我需要进行身份验证以访问xml文件时,我不知道如何从xml文件中删除它 因此,我的xml文件位于url=”ftp://ftp.Dom.com/BlaBla/“user=“我自己”pass=“secret” 在scrapy的文档中,我发现了一些类似于html的东西,并试图使其适应我的需要 这是我的代码:Python 需要使用scrapy AUTH解析XML文件,python,xml,authentication,scrapy,Python,Xml,Authentication,Scrapy,我的问题是,当我需要进行身份验证以访问xml文件时,我不知道如何从xml文件中删除它 因此,我的xml文件位于url=”ftp://ftp.Dom.com/BlaBla/“user=“我自己”pass=“secret” 在scrapy的文档中,我发现了一些类似于html的东西,并试图使其适应我的需要 这是我的代码: class kelly(XMLFeedSpider): name = "kelly" allowed_domains = [] start_urls = ["ftp
class kelly(XMLFeedSpider):
name = "kelly"
allowed_domains = []
start_urls = ["ftp://ftp.Dom.com/BlaBla/"]
itertag='Job'
def __init__(self, name=None, **kwargs):
XMLFeedSpider.__init__(self)
self.secret_users = {}
pipe_import = ImportLaunch()
pipe_import.pipe = Pipe.objects.first()
pipe_import.save()
self.pipe_import = pipe_import
def parse_node(self, response):
import pdb
pdb.set_trace()
return [FormRequest.from_response(response,
formdata={'username': 'myself', 'password': 'secret'},
callback=self.parse_after_log)]
def parse_after_log(self,response):
# check login succeed before going on
if "authentication failed" in response.body:
self.log("Login failed", level=log.ERROR)
return
# We've successfully authenticated, let's have some fun!
else:
return Request(url="ftp://ftp.Dom.com/BlaBla/kelly_polarbear.xml", callback=self.parse_tastypage)
def parse_tastypage(self,response,node):
print 'I passed !!'
但我的信息仍然是
`2012-03-01 12:27:28+0100 [kelly] ERROR: Error downloading <GET ftp://ftp.kellyservices.com`/Polarbear/>: 530 User anonymous cannot log in.
`2012-03-01 12:27:28+0100[kelly]错误:下载错误:530匿名用户无法登录。
谢谢您的帮助一个更简单的方法怎么样
import requests
from lxml import objectify
r = requests.get('http://www.example.com/', auth('username','password'))
root = objectify.fromstring(r.result)
# Now root is an object tree representing your XML
XMLFeedSpider
,但是使用了BaseSpider
,其中XMLFeedSpider
您的.xml文件位于ftp服务器上,但FormRequest会创建HTTP POST请求,该请求通常是在加载带有身份验证表单的页面时通过parse
方法生成的。所以我认为这在这里行不通
有一个用于传递的方法,但看起来它无法与FTP一起工作(尽管您可以尝试)
我认为解决方案是编写您自己的下载中间件,它将使用(尚未尝试)从FTP服务器下载文件:
urllib2.urlopen("ftp://user:password@host.example.com/rest/of/the/url")