1

我有一个从 MySQL 数据库读取 start_urls 并从每个页面抓取未知数量的链接的蜘蛛。我想使用 pipelines.py 使用抓取的链接更新数据库,但我不知道如何将 start_url 重新放入 SQL UPDATE 语句的管道中。

这是有效的蜘蛛代码。

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

这里是 pipeline.py,我想在其中使用 start_url 作为 SQL UPDATE 语句的 WHERE 条件抓取的链接来更新数据库。所以 SQL 语句中的 start_url 是我想要完成的占位符。

import MySQLdb
import MySQLdb.cursors
import hashlib
import re
from scrapy import log
from scrapy.exceptions import DropItem
from twisted.enterprise import adbapi
from youtubephase2.items import Youtubephase2Item

class MySQLStorePipeline(object):
    def __init__(self):
        self.conn = MySQLdb.connect(user='uname', passwd='password', db='YouTubeScrape', host='localhost', charset="utf8", use_unicode=true)
        self.cursor = self.conn.cursor()

def process_item(self, item, spider):
    try:

        self.cursor.execute("""UPDATE SearchResults SET PageURL = %s WHERE ResultURL = start_url[
                    VALUES (%s)""",
                   (item['pageurl']
                                    ))

        self.conn.commit()

    except MySQLdb.Error, e:
        log.msg("Error %d: %s" % (e.args[0], e.args[1]))

    return item

希望我的问题足够清楚。我过去曾成功使用 pipeline.py 将项目插入数据库。

4

1 回答 1

0

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

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']start_urlcursor.execute

于 2017-04-13T03:40:43.353 回答