Python Shell没有运行

2024-06-28 11:19:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在跑步Python.org网站版本2.7 64位Windows Vista 64位使用Scrapy。当我通过命令Shell运行它时,我有一些代码正在工作(除了命令Shell无法识别非Unicode字符的一些问题之外),但是当我尝试通过Python空闲运行脚本时,我得到以下错误消息:

Warning (from warnings module):
  File "C:\Python27\mrscrap\mrscrap\spiders\test.py", line 24
    class MySpider(BaseSpider):
ScrapyDeprecationWarning: __main__.MySpider inherits from deprecated class scrapy.spider.BaseSpider, please inherit from scrapy.spider.Spider. (warning only on first subclass, there may be others)

用于生成此错误的代码是:

^{pr2}$

首先,在命令Shell中正常工作时,出现这种错误的原因是什么?第二,当我按照错误中的说明,将代码中的两个BaseSpider实例都替换为“Spider”时,代码在pythonshell中运行,但不执行任何操作。没有错误,没有打印到日志中,没有错误或警告,没有。在

有人能告诉我为什么这个修订版的代码没有把它的输出输出打印到Python空闲中?在

谢谢


Tags: 代码fromorg命令错误shell跑步空闲
1条回答
网友
1楼 · 发布于 2024-06-28 11:19:20

from scrapy.cmdline import execute添加到导入中

然后放入execute(['scrapy','crawl','wiki'])并运行脚本。在

from scrapy.spider import Spider
from scrapy.selector import Selector
from scrapy.utils.markup import remove_tags
import re
from scrapy.cmdline import execute
class MySpider(Spider):
    name = "wiki"
    allowed_domains = ["wikipedia.org"]
    start_urls = ["http://en.wikipedia.org/wiki/Asia"]

    def parse(self, response):
        titles = response.selector.xpath("normalize-space(//title)")
        for title in titles:

            body = response.xpath("//p").extract()
            body2 = "".join(body)
            print remove_tags(body2)

execute(['scrapy','crawl','wiki'])

相关问题 更多 >