python - 用scrapy爬表,网站有不正常的html代码。

标签 python html scrapy web-crawler

第一篇文章。我感谢任何指导,迫不及待地回馈社区。

我正在尝试使用 scrapy 制作一个爬虫,以从该表中收集数据。

http://www.wikicfp.com/cfp/call?conference=machine%20learning

特别是 session 名称、地点和日期。但是表、tr 和 td 没有类,并且表在另一个表中。

无论我如何编辑我的代码,它都会给我整个页面。

import scrapy


class CfpspiderSpider(scrapy.Spider):
name = 'cfpspider'
allowed_domains = ['http://www.wikicfp.com']
start_urls = ['http://www.wikicfp.com/cfp/call?conference=machine%20learning']

def parse(self, response):
    div = response.css("div.contsec")

    for table in div:
        print(table.css("table")[3].css.extract_first())

稍后,我将努力让它进入下一页并输出 csv 或 json,但现在我正在尝试获取此表的部分内容。我在 scrapy shell 中测试了一些命令,但我的知识不足。 谢谢

最佳答案

从源码来看,页面的结构是这样的:

div class="contsec"
| center
| | form
| | | table
| | | | tr
| | | | tr
| | | | tr
| | | | | td
| | | | | | table id="the droids you are looking for"
| | | | tr

编辑:试试这个

def parse(self, response):
    divs = response.css("div.contsec")
    for div in divs:
            table = div.css("table")[3]
            headers = table.css("tr")[0].css("td::text").extract()
            # print("<table headers>")
            print("\t".join(headers))
            # print("</table headers>")
            for row in table.css("tr")[1:]:
                    row_data = row.css("td::text").extract()
                    print("\t".join(row_data))

关于python - 用scrapy爬表,网站有不正常的html代码。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52865617/

相关文章:

python libtorrent 使用磁铁 URI 时我应该等待元数据多长时间?

python - 是否有等同于 PyVirtualDisplay 的 Windows

html - Bootstrap : Get three thumbnail images inline

Stack Overflow 中从下一页到 scrapy 的 Xpath

python - Apache Nutch 的任何替代品?

python - 使用celery将任务从组件A发送到组件B

python - 使用Python将Excel数据导出到Google Sheets

html - 带有 CSS 和 HTML 的水平家谱 - 反向

performance - IFrame 的效率如何?

python - scrapy 有没有办法在不使用 -o -t 参数的情况下打印 json 文件