我有一个spider,它从MySQL数据库中读取start_url,并从每个页面抓取未知数量的链接。我想用管道.py更新数据库与刮掉的链接,但我不知道如何将start\u url恢复到sqlupdate语句的管道中。在
这是一个蜘蛛代码。在
import scrapy
import MySQLdb
import MySQLdb.cursors
from scrapy.http.request import Request
from youtubephase2.items import Youtubephase2Item
class youtubephase2(scrapy.Spider):
name = 'youtubephase2'
def start_requests(self):
conn = MySQLdb.connect(user='uname', passwd='password', db='YouTubeScrape', host='localhost', charset="utf8", use_unicode=True)
cursor = conn.cursor()
cursor.execute('SELECT resultURL FROM SearchResults;')
rows = cursor.fetchall()
for row in rows:
if row:
yield Request(row[0], self.parse)
cursor.close()
def parse(self, response):
for sel in response.xpath('//a[contains(@class, "yt-uix-servicelink")]'):
item = Youtubephase2Item()
item['pageurl'] = sel.xpath('@href').extract()
yield item
这是管道.py在这里,我想用start_url作为sqlupdate语句的where条件来更新数据库。所以SQL语句中的start_url是我想要完成的任务的占位符。在
^{pr2}$希望我的问题足够清楚。我用过管道.py在过去成功地将项插入到数据库中。在
您可以使用
meta
请求参数在相关请求和项之间传递相关信息:现在,您还可以使用
response.url
,但这可能会因为重定向或其他东西而改变,因此它以后可能会与数据库中的有所不同。在最后,您必须更新管道,以将
item['start_url']
作为cursor.execute
中的start_url
参数进行传递相关问题 更多 >
编程相关推荐