爬行表有刮擦,网站有不寻常的html代码。

2024-05-18 13:56:36 发布

您现在位置:Python中文网/ 问答频道 /正文

第一个岗位。我很感激任何指导,迫不及待地想回馈社会。你知道吗

我正在尝试使用scrapy制作一个爬虫,从这个表中收集数据。你知道吗

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

特别是会议名称、地点和日期。但是表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,但目前我正在尝试获取此表的部分内容。我已经在ScrapyShell中测试了一些命令,但是我的知识还不够。 谢谢


Tags: divcomhttpresponsewwwtablecallcss
1条回答
网友
1楼 · 发布于 2024-05-18 13:56:36

从源代码的外观来看,页面的结构如下所示:

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))

相关问题 更多 >

    热门问题