每次启动时更新mysql的Scrapy管道

2024-09-30 14:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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在过去成功地将项插入到数据库中。在


Tags: pyimportself数据库url管道链接语句
1条回答
网友
1楼 · 发布于 2024-09-30 14:25:32

您可以使用meta请求参数在相关请求和项之间传递相关信息:

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, meta=dict(start_url=row[0]))
    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()
        item['start_url'] = response.meta['start_url']
        yield item

现在,您还可以使用response.url,但这可能会因为重定向或其他东西而改变,因此它以后可能会与数据库中的有所不同。在

最后,您必须更新管道,以将item['start_url']作为cursor.execute中的start_url参数进行传递

相关问题 更多 >