Scrapy从网站提取表

时间:2017-10-25 17:01:10

标签: python html web-scraping scrapy

我是Python新手,我正在尝试编写一个脚本来从这个page中提取数据。使用scrapy,我编写了以下代码:

JwtConsumer

然而,这并没有刮掉任何东西。你有什么想法 ? 感谢

1 个答案:

答案 0 :(得分:1)

通过向http://www.dairy.com/market-prices/?page=quote&sym=DAH15&mode=i发出请求,页面http://shared.websol.barchart.com/quotes/quote.php?page=quote&sym=DAH15&mode=i&domain=blimling&display_ice=&enabled_ice_exchanges=&tz=0&ed=0上的表格会动态添加到DOM

你应该废弃第二个链接而不是第一个链接。由于scrapy.Request只会返回html源代码,而不会返回使用javascript添加的内容。

<强>更新

以下是提取表格数据的工作代码

import scrapy

class dairySpider(scrapy.Spider):
    name = "dairy_price"

    def start_requests(self):
        urls = [
            "http://shared.websol.barchart.com/quotes/quote.php?page=quote&sym=DAH15&mode=i&domain=blimling&display_ice=&enabled_ice_exchanges=&tz=0&ed=0",
        ]

        for url in urls:
            yield scrapy.Request(url=url, callback=self.parse)


    def parse(self, response):
        for row in response.css(".bcQuoteTable tbody tr"):
            print row.xpath("td//text()").extract()

确保您修改了settings.py文件并将ROBOTSTXT_OBEY = True更改为ROBOTSTXT_OBEY = False