python - Scrapy Spider解析器调用函数

标签 python xml scrapy

解析器爬取完数据后如何调用 writeXML?目前我可以看到数据抓取,但看不到输出文件。我尝试在 writeXML 下打印也没有输出。

下面是我的代码:

class FriendSpider(BaseSpider):
    # identifies of the Spider
    name = "friend"
    count = 0 
    allowed_domains = ["example.com.us"]
    start_urls = [
        "http://example.com.us/biz/friendlist/"
    ]

    def start_requests(self):
        for i in range(0,1722,40):
            yield self.make_requests_from_url("http://example.com.us/biz/friendlist/?start=%d" % i)

    def parse(self, response):
        response = response.replace(body=response.body.replace('<br />', '\n')) 
        hxs = HtmlXPathSelector(response)
        sites = hxs.select('//ul/li')
        items = []

        for site in sites:
            item = Item()
            self.count += 1
            item['id'] = str(self.count)
            item['name'] = site.select('.//div/div/h4/text()').extract()
            item['address'] = site.select('h4/span/text()').extract()
            item['review'] = ''.join(site.select('.//div[@class="review"]/p/text()').extract())
            item['birthdate'] = site.select('.//div/div/h5/text()').extract()

            items.append(item)
        return items

    def writeXML(self, items):
        root = ET.Element("Test")
        for item in items:
            item= ET.SubElement(root,'item')
            item.set('id', item['id'])
            address= ET.SubElement(item, 'address')
            address.text = item['address']
            user = ET.SubElement(item, 'user')
            user.text = item['user']
            birthdate= ET.SubElement(item, 'birthdate')
            birthdate.text = item['birthdate']
            review = ET.SubElement(item, 'review')
            review.text = item['review']

        # wrap it in an ElementTree instance, and save as XML
        file = open("out.xml", 'w')
        tree = ET.ElementTree(root)
        tree.write(file,xml_declaration=True,encoding='utf-8',method="xml")

最佳答案

要使用内置 XML 导出器进行输出,请尝试以下命令:

scrapy crawl friend -o items.xml -t xml

如果输出不符合您的喜好,那么您可以尝试使用 XMLExporter class 创建自己的导出器作为基础。

关于python - Scrapy Spider解析器调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15863640/

相关文章:

java - 从 python 调用 Java 应用程序时无法读取文件

python - 使用 linux 服务和 python 文件每 12 小时重新启动 apache

java - 将 HTML 表格转换为 XML

python - scrapy中处理XMLHttpRequest

python - "Replacing"具体函数调用

python - 如何修复 numpy 浮点运算产生不精确结果的问题?

java - 在 XML 文档中查找所有 namespace 声明 - xPath 1.0 与 xPath 2.0

python - 从 xml 中提取文本时保留换行符

python - Scrapy:创建爬行索引页面并保存每个对应链接的整个 HTML 页面的 Spider

python - 我的第一个 Scrapy Spider 不支持 MySQL 数据库