首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >美丽的汤没有检测到td-tag的结尾

问美丽的汤没有检测到td-tag的结尾
EN

Stack Overflow用户
提问于 2020-08-20 15:49:39
回答 1查看 45关注 0票数 0

我正在收集我的faculty的所有考试日期,以跟踪变化等。

我的代码:

代码语言:javascript
复制
from bs4 import BeautifulSoup
import requests
import csv


data = requests.get('https://www.wiwi.kit.edu/pruefungstermine.php')

soup = BeautifulSoup(data.text, 'lxml')


table = soup.find('tbody').find_all('tr') #finds table with relevant information and returns a list with all entries (is working)

first_row = ('Prüfung', 'Prüfer', 'Datum', 'Zeit/Ort') #header (in German but doesn't matter)

exams = []

for row in table: #looping through every tr
    content = row.find_all('td')
    exam_name = content[0].find('a').text.strip()
    lecturer = content[1].text.strip()
    date = content[2].text.strip()
    time_location = content[3].text.replace('\n', ', ').strip()

    exam = (exam_name, lecturer, date, time_location)
    exams.append(exam)


with open('exams.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerow(first_row)
    for row in exams:
        writer.writerow(row)

(可能只能循环一次,但这不应该是问题所在)

它在一定程度上工作得很好,但是它没有检测到关闭,最后一个表项看起来像这样:

代码语言:javascript
复制
Organisationsmanagement,Lindstädt,13.02.2020,"14.30 - 17.30: Audimax, Neue Chemie</span></td><td class=""dialog""><a href=""/m/ics.php?pruef_id=618550&pIntervall=2020""><img src=""/img/ical_icon.png"" width=""16"" height=""16"" alt=""iCal Eintrag"" /></a></td></tr><tr id=""618551"" title=""&nbsp;""><td><a href=""pruefungstermin.php?func=exam&pruef_id=618551&pIntervall=2020"">Problemlösung, Kommunikation und Leadership (PKL)</a></td><td>Lindstädt</td><td>13.02.2020</td><td>14.30 - 17.30: Audimax, <style=""color:#ff0000;"">Neue Chemie</span></td><td cl ........

这显然是最后一个表条目,因为Beautiful Soup不知何故没有检测到,下面的html代码放在这里。

此条目的html代码:

代码语言:javascript
复制
<tr id="618552" title="&nbsp;" role="row" class="odd"><td class="sorting_1"><a href="pruefungstermin.php?func=exam&amp;pruef_id=618552&amp;pIntervall=2020">Unternehmensführung und Strategisches Management </a></td><td>Lindstädt</td><td>13.02.2020</td><td>14.30 - 17.30: Audimax, <style="color:#ff0000;">Neue Chemie</style="color:#ff0000;"></td><td class="dialog"><a href="/m/ics.php?pruef_id=618552&amp;pIntervall=2020"><img src="/img/ical_icon.png" width="16" height="16" alt="iCal Eintrag"></a></td></tr>

在这篇文章之前,有人能说出为什么它是有效的吗?

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-20 16:01:56

我认为这是由于Neue Chemie周围的格式错误的样式标签造成的

代码语言:javascript
复制
<style="color:#ff0000;">Neue Chemie</style="color:#ff0000;">

这是无效的html。删除样式标签可能会得到您想要的结果。如果可行,您可以尝试保留style标记,但使其成为格式正确的标记,而不在结束标记中包含任何附加信息,该结束标记应始终读取</style>

查看源代码后,它确实是格式错误的HTML:

​

在这里,你有一个结束,但没有打开跨度。相反,你有了一个机会。

根据文件的其余部分,看起来您想要的是一个具有样式属性的开放跨度,比如:<span style="something;">text</span>

其中有相当多的问题需要纠正。您可以使用搜索/替换来完成此操作:

搜索:<style="color:#ff0000

替换:<span style="color:#ff0000

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63500589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档