我正在尝试从my.py文件导入代码,这是one.py
上我当前代码的延续,但不断给我“exceptions.NameError: name 'main' is not defined
”任何帮助都会很棒。还有另一种方法可以继续anotherfile.py
上的代码吗?
import scrapy
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import HtmlXPathSelector
from crawler.items import CrawlertItem
from scrapy.contrib.spiders import SitemapSpider
from scrapy.log import *
from crawler.settings import *
from crawler.items import *
from urlparse import urlparse
import re
class GottaRent(CrawlSpider):
name = "test"
allowed_domains = ["www.example.com"]
start_urls = ["http://www.example.com"]
rules = [
Rule(
SgmlLinkExtractor(
allow=(),
deny=(r'/files/',)
),
callback ="parse",
follow=True
)
]
def parse(self, response):
hxs = HtmlXPathSelector(response)
main = hxs.xpath("//body")
for main in main:
item = CrawlertItem()
item['title'] = ''.join(main.xpath("//title//text()").extract())
import sys, os
sys.path.append(os.path.abspath('C:\crawler\__init__'))
from two import *
yield item
item['address'] = ''.join(main.xpath("//address//text()").extract())
答案 0 :(得分:0)
通常,您应该在文件的开头执行所有导入,而不是在循环内部。您的two.py
示例会抛出该错误,因为模块不包含变量main
(并且item
也会丢失)。
导入其他文件时,它应该只包含可以在其他文件中使用的函数和类。您不应该只是将代码“继续”到其他文件,而是可以在那里移动相关函数或类的组。