Beautiful Soup 没有检测到 td-tag 的结尾
Beautiful Soup doesn't detect end of td-tag
我正在收集我的所有考试日期 faculty 以跟踪变化等
我的代码:
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)
(可能只能循环一次,但这不应该是这里的问题)
它在某一点上工作正常,但后来它没有检测到关闭,最后一个 table 条目看起来像这样:
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="" ""><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 ........
这显然是最后一个 table 条目,因为 Beautiful Soup 不知何故没有检测到,下面的 html 代码放在这里。
本条目的html代码:
<tr id="618552" title=" " role="row" class="odd"><td class="sorting_1"><a href="pruefungstermin.php?func=exam&pruef_id=618552&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&pIntervall=2020"><img src="/img/ical_icon.png" width="16" height="16" alt="iCal Eintrag"></a></td></tr>
有人能说说为什么它在这个条目之前一直有效吗?
提前致谢
我想这是由于 Neue Chemie
:
周围的格式错误的标签造成的
<style="color:#ff0000;">Neue Chemie</style="color:#ff0000;">
这是无效的 html。删除样式标签可能会让您获得想要的结果。如果可行,您可以尝试保留样式标签,但将其设为 properly-formed 标签,而在结束标签中不包含任何附加信息,该标签应始终显示为 </style>
看了源码,确实是格式错误HTML:
这里有一个关闭跨度但没有打开跨度。相反,你有一个空缺 .
根据文件的其余部分,您似乎想要的是一个带有如下样式属性的开放跨度:
<span style="something;">text</span>
其中有不少需要更正。您可以使用 search/replace:
搜索:<style="color:#ff0000
替换:<span style="color:#ff0000
我正在收集我的所有考试日期 faculty 以跟踪变化等
我的代码:
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)
(可能只能循环一次,但这不应该是这里的问题)
它在某一点上工作正常,但后来它没有检测到关闭,最后一个 table 条目看起来像这样:
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="" ""><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 ........
这显然是最后一个 table 条目,因为 Beautiful Soup 不知何故没有检测到,下面的 html 代码放在这里。
本条目的html代码:
<tr id="618552" title=" " role="row" class="odd"><td class="sorting_1"><a href="pruefungstermin.php?func=exam&pruef_id=618552&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&pIntervall=2020"><img src="/img/ical_icon.png" width="16" height="16" alt="iCal Eintrag"></a></td></tr>
有人能说说为什么它在这个条目之前一直有效吗?
提前致谢
我想这是由于 Neue Chemie
:
<style="color:#ff0000;">Neue Chemie</style="color:#ff0000;">
这是无效的 html。删除样式标签可能会让您获得想要的结果。如果可行,您可以尝试保留样式标签,但将其设为 properly-formed 标签,而在结束标签中不包含任何附加信息,该标签应始终显示为 </style>
看了源码,确实是格式错误HTML:
这里有一个关闭跨度但没有打开跨度。相反,你有一个空缺 .
根据文件的其余部分,您似乎想要的是一个带有如下样式属性的开放跨度:
<span style="something;">text</span>
其中有不少需要更正。您可以使用 search/replace:
搜索:<style="color:#ff0000
替换:<span style="color:#ff0000