Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 美丽的汤不';t检测td标签的结束_Python_Html_Beautifulsoup_Web Crawler - Fatal编程技术网

Python 美丽的汤不';t检测td标签的结束

Python 美丽的汤不';t检测td标签的结束,python,html,beautifulsoup,web-crawler,Python,Html,Beautifulsoup,Web Crawler,我正在收集我的所有考试日期,以跟踪变化等 我的代码: 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 tab

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

我的代码:

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)
(可能只能循环一次,但这不应该是问题所在)

它在某一点上工作正常,但没有检测到关闭,最后一个表项如下所示:

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

OrganizationsManagement,Lindstädt,13.02.2020,“14.30-17.30:Audimax,Neue ChemieLindstädt13.02.202014.30-17.30:Audimax,Neue Chemie我认为这是由于
Neue Chemie
周围的错误样式标签造成的:

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

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

这里有一个闭合段,但没有开口段,而是一个开口段

根据文件的其余部分,看起来您想要的是一个带有样式属性的开口跨距,如:
text

其中有许多问题需要纠正。您可以通过搜索/替换来解决:


搜索:
您可以提供整个页面的链接,或者在您提供的数据中包含上一行吗?您还可以删除“Neue Chemie”周围格式错误的样式标记,看看这会给您带来什么?
打印(len(考试))
显示467,与下表中的数字匹配。Neue Chemie不工作。如果没有span,它工作。添加了链接。这意味着它检测到的是正确的。但为什么所有html代码都在一个字段中?我删除了结束标记中的附加信息,但它仍然不工作。BS不知何故,也不是一直都在输入。这可能就是问题所在吗?因为它被视为一个字符串?是的,它当然可能是一个问题,但我非常怀疑它是否是一个bs4问题。更有可能的是,原始文档中的HTML格式更不正确。你能提供文件吗?当我看到上面的样式标记时,我假设还有其他错误……将replace命令添加到我的脚本中。它现在正在工作。非常感谢你!
<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>