![](/img/trans.png)
[英]In Python scrapy, With Multiple projects, How Do You Import A Class Method From One project Into Another project?
[英]If you are extending a class in python how do you import another class and use it?
因此,我在玩Scrapy,这是一组允许您进行Web抓取的类,我想将一些数据放入数据库中,但是在扩展scrapy库的同时导入MySQL方法的工作很麻烦。
这是我的代码:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector
from scrapy.http import Request
import MySQLdb
class test(BaseSpider): #if i don't extend the class the MySQL works, but the Scrapy functionallity does not.
name = "test"
allowed_domains = ["some-website.com"] #i know this is probibly not a real websit... just using it as an example.
start_urls = [
"http://some-website.com",
]
db = MySQLdb.connect(
host = 'localhost',
user = 'root',
passwd = '',
db = 'scrap'
)
#cursor = db.cursor()
def parse(self, response):
hxs = HtmlXPathSelector(response)
for title in hxs.select('//a[@class="title"]/text()').extract():
print title
cursor.execute("INSERT INTO `scrap`.`shows` (id, title) VALUES (NULL , '"+title+"');")
我还是python的菜鸟,所以任何帮助将不胜感激。
也许您应该定义self.cursor
?
这样,光标将可以在类方法上使用。 我不了解scrapy,但是您很可能应该在类test的__init__
方法或get_cursor
方法上执行此操作(可能应该将其重命名为Test)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.