所以我在玩Scrapy,這是一組允許你做網頁抓取的類,我想把一些數據放到數據庫中,但是我在擴展scrapy庫的時候會導入MySQL方法。如果你在Python中擴展一個類,你如何導入另一個類並使用它?
這裏是我的代碼:
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所以任何幫助將不勝感激。
你能解決你的縮進問題嗎? –