scrapy代码无法正常工作

时间:2016-03-30 08:30:04

标签: scrapy

...我有一个在shell中运行的scrapy代码,但是当我尝试将其导出到csv时,它会返回一个空文件。当我没有进入链接并尝试解析描述时,它会导出数据

    import scrapy

class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["dmozs.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
    ]

    def parse(self, response):
        filename = response.url.split("/")[-2] + '.html'
        with open(filename, 'wb') as f:
            f.write(response.body)

2 个答案:

答案 0 :(得分:4)

import scrapy

class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
    ]

    def parse(self, response):
        filename = response.url.split("/")[-2] + '.html'
        with open(filename, 'wb') as f:
            f.write(response.body)
只是你错过了类型 allowed_domains = [" dmozs.org"] allowed_domains = [" dmoz.org"]

答案 1 :(得分:1)

请更改代码allowed_domains = ["dmoz.org"]