Итак, я поиграюсь с Scrapy, который представляет собой набор классов, позволяющих вам выполнять очистку веб-страниц, и я хотел выбросить некоторые данные в базу данных, но у меня сложный импорт методов MySQL при расширении scrapyбиблиотека.
вот мой код:
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, поэтому любая помощь будет принята с благодарностью.