Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 循环浏览BeautifulSoup列表,并将每个列表解析为HTML标记和数据问题_Python 3.x_Beautifulsoup_Html Parsing - Fatal编程技术网

Python 3.x 循环浏览BeautifulSoup列表,并将每个列表解析为HTML标记和数据问题

Python 3.x 循环浏览BeautifulSoup列表,并将每个列表解析为HTML标记和数据问题,python-3.x,beautifulsoup,html-parsing,Python 3.x,Beautifulsoup,Html Parsing,Python3程序员,BeautifulSoup和HTMLParser的新手。我使用BeautifulSoup从HTML文件中获取所有定义列表数据,并尝试将dt数据和dd数据作为键值对相应地存储到python字典中。我的HTML文件(List_page.HTML)是: 输出为: 这将按预期正确输出内容。但是,当我用for循环替换第1部分(或第2部分)时,它开始出错: 例如,代码: # Similar change for part 2 for dt in dts: parser.fee

Python3程序员,BeautifulSoup和HTMLParser的新手。我使用BeautifulSoup从HTML文件中获取所有定义列表数据,并尝试将dt数据和dd数据作为键值对相应地存储到python字典中。我的HTML文件(List_page.HTML)是:

输出为:

这将按预期正确输出内容。但是,当我用for循环替换第1部分(或第2部分)时,它开始出错:

例如,代码:

# Similar change for part 2
for dt in dts:
    parser.feed(str(dts[0]).replace('\n', ''))

在这种情况下,只告诉我余弦的定义,而不是正弦。有了2个项目,我就可以不用循环完成这项工作。但是如果我有更多的东西呢?所以我想知道一个正确的方法。谢谢

您将使用
dts[0]
在for循环中获得dts的第一个元素,而不是使用循环更新索引。将其更改为:

for i in range(len(dts)):
    parser.feed(str(dts[i]).replace('\n', ''))


您将使用
dts[0]
在for循环中获得dts的第一个元素,而不是使用循环更新索引。将其更改为:

for i in range(len(dts)):
    parser.feed(str(dts[i]).replace('\n', ''))

for i in range(len(dts)):
    parser.feed(str(dts[i]).replace('\n', ''))
for i in range(len(dds)):
    parser.feed(str(dds[i]).replace('\n', ''))