我在解析来自网站的 xml 数据以识别其最近更新日期时遇到问题。这是我正在使用的代码:
def fetch_dates(self, response):
sitemap = scrapy.selector.XmlXPathSelector(response)
sitemap.register_namespace(
# ns is just a namespace and the second param should be whatever the
# xmlns of your sitemap is
'ns', 'http://www.sitemaps.org/schemas/sitemap/0.9'
)
# this gets you a list of all the "loc" and "last modified" fields.
locsList = sitemap.select('//ns:loc/text()').extract()
lastModifiedList = sitemap.select('//ns:lastmod/text()').extract()
# zip() the 2 lists together
pageList = list(zip(locsList, lastModifiedList))
for page in pageList:
if os.path.exists('1url-to-date.csv'):
append_write = 'a'
else:
append_write = 'w'
with open('1url-to-date.cav', append_write) as url_f:
url_f.write(locsList + "&,&" + lastModifiedList + "/n")
return Item()
但它没有返回任何日期值,甚至没有写入我的文件。所以代码显然有问题。运行时我没有看到任何错误,但我没有返回任何内容。关于如何修复它的任何建议?
我最终要寻找的是网络爬虫找到的 HTML 页面和更新日期的列表。如果没有可用的日期,我将使用今天的日期,然后是自上次更新以来的天数。