【发布时间】:2013-12-05 19:00:48
【问题描述】:
我正在尝试使用 spider.py 使用 MYSQL 表中的 SELECT 填充 start_url。当我运行“scrapy runspider spider.py”时,我没有得到任何输出,只是它没有错误地完成。
我已经在 python 脚本中测试了 SELECT 查询,并且 start_url 填充了 MYSQL 表中的条目。
spider.py
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
import MySQLdb
class ProductsSpider(BaseSpider):
name = "Products"
allowed_domains = ["test.com"]
start_urls = []
def parse(self, response):
print self.start_urls
def populate_start_urls(self, url):
conn = MySQLdb.connect(
user='user',
passwd='password',
db='scrapy',
host='localhost',
charset="utf8",
use_unicode=True
)
cursor = conn.cursor()
cursor.execute(
'SELECT url FROM links;'
)
rows = cursor.fetchall()
for row in rows:
start_urls.append(row[0])
conn.close()
【问题讨论】:
标签: python mysql scrapy web-crawler