从 MySQL table 抓取 Start_URLs

Scrapy fetch Start_URLs from MySQL table

我在从 MySQL 中的 table 获取 Scrapy 的 start_urls 到 运行 时遇到问题。

MySQL 有一个名为 "scrapy" 的数据库,一个名为 "urls" 的 table 有一个名为 "url" 的列,每行有一个 URL 刮。

这是我目前的代码,但我觉得我在某处遗漏了一个概念:

# -*- coding: utf-8 -*-
import scrapy
import datetime
import urlparse
import socket
import MySQLdb
from scrapy.loader import ItemLoader
from example.items import exampleitem


class instantdbSpider(scrapy.Spider):
    name = 'instantdb'
    allowed_domains = ['example.com']
    def start_requests(self):
        conn = MySQLdb.connect(
            user='root',
            passwd='Password!',
            db='scrapy',
            host='localhost',
            charset="utf8",
            use_unicode=True,
            cursorclass=MySQLdb.cursors.DictCursor
        )
        cursor = conn.cursor()
        cursor.execute('SELECT * FROM urls')
        rows = cursor.fetchall()
        for row in rows:
            url = row["url"]
        yield Request(url=url)

    def parse(self, response):
        l = ItemLoader(item=exampleitem(), response=response)

        #Scrape Fields
        l.add_xpath('title', '//html/head/title/text()')
        l.add_xpath('sku', '//*[@id="js-zoom-image-container"]/div[5]/h2/strong/text()')
        l.add_xpath('price', '//*[@id="main-content"]/div/div[1]/div[1]/div[1]/div/div[1]/div[2]/p/text()[1]')
        l.add_xpath('product_title', '//html/body/div[1]/span[4]/text()')
        l.add_xpath('image_url', '//*[@id="main-content"]/div/div[1]/div[1]/div[2]/div[1]/div[3]/div[1]/a/img/@src')
        l.add_xpath('description', '//*[@id="main-content"]/div/div[1]/div[1]/div[2]/div[7]')

        # Administration Fields
        l.add_value('url', response.url)
        l.add_value('project', self.settings.get('BOT_NAME'))
        l.add_value('spider', self.name)
        l.add_value('server', socket.gethostname())
        l.add_value('date', datetime.datetime.now())

        return l.load_item()

任何帮助将不胜感激,因为我似乎在兜圈子。谢谢

您遇到了缩进问题:

for row in rows:
    url = row["url"]
    yield Request(url=url)