【问题标题】:How to get xpath from different urls,returned by start_requests method如何从不同的 url 获取 xpath,由 start_requests 方法返回
【发布时间】:2015-01-06 08:02:38
【问题描述】:

这是我的scrapy代码:

import scrapy
from scrapy.spider import BaseSpider
from scrapy.selector import Selector
import MySQLdb


class AmazonSpider(BaseSpider):
    name = "amazon"
    allowed_domains = ["amazon.com"]
    start_urls = []

    def parse(self, response):
        print self.start_urls

    def start_requests(self):
        conn = MySQLdb.connect(user='root',passwd='root',db='mydb',host='localhost')
        cursor = conn.cursor()
        cursor.execute(
            'SELECT url FROM products;'
            )
        rows = cursor.fetchall()
        for row in rows:
            yield self.make_requests_from_url(row[0])
        conn.close()

如何获取start_requests函数返回的url的xpath?

注意:url是不同的域,不一样的。

【问题讨论】:

    标签: python xpath scrapy web-crawler


    【解决方案1】:

    yield 使 start_requests 函数成为生成器。使用for 循环获取从它返回的每个结果。

    像这样:

    ...
    my_spider = AmazonSpider()
    for my_url in my_spider.start_requests():
        print 'we get URL: %s' % str(my_url)
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 2016-02-04
      相关资源
      最近更新 更多