python - Scrapy 从网站上提取表格

标签 python html web-scraping scrapy

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

import scrapy

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

    def start_requests(self):
        urls = [
            'http://www.dairy.com/market-prices/?page=quote&sym=DAH15&mode=i',

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




    def parse(self, response):
        for rows in response.xpath("//tr"):
            yield {
                'text': rows.xpath(".//td/text()").extract().strip('. \n'),

                }

但是,这并没有抓取掉任何东西。你有什么想法 ? 谢谢

最佳答案

页面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

关于python - Scrapy 从网站上提取表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46938138/

相关文章:

python - count() 方法中的整数到 bool 值的转换

python - 从python的urllib2访问netflix api导致500错误

html - 使用 CSS 在 HTML 页面上定位三列

python - 使用 requests.post() 从网页中抓取文本

python - 如何用python获取表标签1和表标签2之后的所有<li>?

python - Django 中 CBV 的设计

python - 如何将值传递给 pyparsing parseactions?

javascript - 显示表单上所有选定单选按钮的值

jquery - Bootstrap tab 根据内容自动设置tab高度

javascript - 如何在 Node.js 中使用代理