【问题标题】:Scrapy Parse table and skip table rows using their class namesScrapy 使用它们的类名解析表并跳过表行
【发布时间】:2015-09-12 04:07:17
【问题描述】:
import scrapy
from mystocks.items import MystocksItem
from scrapy.selector import Selector
import datetime

class Synergyspider(scrapy.Spider):
    name = "synergyspider"
    allowed_domains = ["http://live.mystocks.co.ke/price_list/"]


    def parse(self, response):

        sel = Selector(response)


        head = sel.xpath('//*[@id="main"]/h2')
        rows_r1 = sel.xpath('//tr[@class = "row r1"]')



        items = []



        for row in rows_r1:

            item = MystocksItem()
            item['date'] = head.xpath('text()').extract()[0]
            item['code'] = rows_r1.xpath('./td[1]/a/text()').extract()[0]
            item['name'] = rows_r1.xpath('./td[2]/text()').extract()[0]
            item['last12_low'] = rows_r1.xpath('./td[3]/text()').extract()[0]
            item['last12_high'] = rows_r1.xpath('./td[4]/text()').extract()[0]
            #item['day_low'] = rows_r1.xpath('./td[5]/text()').extractf()[0]
            item['day_high'] = rows_r1.xpath('./td[6]/text()').extract()[0]
            item['price'] = rows_r1.xpath('./td[7]/text()').extract()[0]
            item['previous'] = rows_r1.xpath('./td[8]/text()').extract()[0]
            item['change'] = rows_r1.xpath('./td[9]/text()').extract()[0]
            item['percentChange'] = rows_r1.xpath('./td[10]/text()').extract()[0]
            item['volume'] = rows_r1.xpath('./td[12]/text()').extract()[0]
            item['adjustedPrice'] = rows_r1.xpath('./td[13]/text()').extract()[0]

            items.append(item)


        return items

我试图解析的网址是http://live.mystocks.co.ke/price_list/20140402 我想遍历“row r1”和“row r0”类的行,但跳过“row”类的行。

【问题讨论】:

  • 我的输出基本上循环在第一行 [synergyspider] DEBUG: Scraped from live.mystocks.co.ke/price_list/20140402> {'adjustedPrice': u'-', 'change': u'-', ' code': u'EGAD', 'date': u'2014 年 4 月 2 日星期三的价格表和交易摘要', 'day_high': u'29.00', 'last12_high': u'100.00', 'last12_low': u'30.00', 'name': u'Eaagads', 'percentChange': u'-', 'previous': u'29.00', 'price': u'29.00', 'volume': u'-'}

标签: python dynamic web-scraping scrapy-spider


【解决方案1】:

发现我在错误的变量上循环,在行上循环而不是 row_r1

【讨论】:

    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    相关资源
    最近更新 更多