gpt4 book ai didi

python - 尝试从 html 表 python 添加某个值作为键并为该键添加多个值

转载 作者:行者123 更新时间:2023-12-01 01:54:30 25 4
gpt4 key购买 nike

所以我使用 beautifulsoup 和 python 来获取 html 页面中每个表的标题以及每个表的多个“a”标签值。我想将我获得的每个 header 添加为字典的键值,并将每个“a”标记值(链接)添加为该键的值,无论其中是否有 1、2、3 等链接 table 。现在,我有一个标题和链接列表,但我无法为该列表中的一个索引添加多个链接,因此当一个表中有多个链接时,我只向列表添加一个链接。我能够得到标题并将它们添加为字典的键,但我正在努力获取表中的每个链接并将它们添加到该键中。因此,字典的输出将类似于 {'header1': [url, url, url], 'header2': [url, url], 'header3': [url, url, url], "}。这是到目前为止我的代码:

from bs4 import BeautifulSoup as soup

s = soup(open('april12016.html').read(), 'lxml')

headers = []
links = []
head = {}

for table in s.find_all('table', attrs={'class':'affected_software'}):
headers.append(table.select('b')[0].text)
for a in table.find_all('a'):
links.append(a.get('href'))

keys = range(len(headers))
for i in keys:
for x in headers:
head[i] = x

这是一些 HTML 源代码

<!-- Affected software tables -->
</table> <table class="affected_software" border=1 cellpadding=0 width="99%">
<thead style="background-color: #ededed">
<tr>
<td colspan="5"><b>Internet Explorer 11 on Windows 10 Version 1511 for x64-based Systems</b></td>
</tr>
</thead>
<tr>
<td><b>CVE ID</b></td>
<td><b>KB Article</b></td>
<td><b>Restart Required</b></td>
<td><b>Severity</b></td>
<td><b>Impact</b></td>
</tr>
<tr>
<td>CVE-2016-0154</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=3147458; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3147458; SubType=Security Update}">@{ID=3147458; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3147458; SubType=Security Update}</a><br></td>
<td>Yes<br></td>
<td>Critical<br></td>
<td>Remote Code Execution<br></td>
</tr> <tr>
<td>CVE-2016-0160</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=3147458; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3147458; SubType=Security Update}">@{ID=3147458; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3147458; SubType=Security Update}</a><br></td>
<td>Yes<br></td>
<td>Important<br></td>
<td>Remote Code Execution<br></td>
</tr> <tr>
<td>CVE-2016-0162</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=4015219; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4015219; SubType=Security Update}">@{ID=4015219; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4015219; SubType=Security Update}</a><br></td>
<td>Yes<br></td>
<td>Moderate<br></td>
<td>Information Disclosure<br></td>
</tr> <tr>
<td>CVE-2016-0166</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=3147458; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3147458; SubType=Security Update}">@{ID=3147458; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB3147458; SubType=Security Update}</a><br></td>
<td>Yes<br></td>
<td>Critical<br></td>
<td>Remote Code Execution<br></td>
</tr>
</table> <table class="affected_software" border=1 cellpadding=0 width="99%">
<thead style="background-color: #ededed">
<tr>
<td colspan="5"><b>Internet Explorer 11 on Windows 7 for 32-bit Systems Service Pack 1</b></td>
</tr>
</thead>
<tr>
<td><b>CVE ID</b></td>
<td><b>KB Article</b></td>
<td><b>Restart Required</b></td>
<td><b>Severity</b></td>
<td><b>Impact</b></td>
</tr>
<tr>
<td>CVE-2016-0154</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=3148198; URL=https://www.microsoft.com/downloads/details.aspx?familyid=742fffb3-232c-4082-b054-853b6ad48406; SubType=Security Update}">@{ID=3148198; URL=https://www.microsoft.com/downloads/details.aspx?familyid=742fffb3-232c-4082-b054-853b6ad48406; SubType=Security Update}</a><br></td>
<td>Yes<br></td>
<td>Critical<br></td>
<td>Remote Code Execution<br></td>
</tr> <tr>
<td>CVE-2016-0162</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=4015549; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4015549; SubType=Monthly Rollup}">@{ID=4015549; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4015549; SubType=Monthly Rollup}</a><br><br /><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=4014661; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4014661; SubType=IE Cumulative}">@{ID=4014661; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4014661; SubType=IE Cumulative}</a><br></td>
<td>Yes<br><br />Yes<br></td>
<td>Moderate<br></td>
<td>Information Disclosure<br></td>
</tr>
</table> <table class="affected_software" border=1 cellpadding=0 width="99%">
<thead style="background-color: #ededed">
<tr>
<td colspan="5"><b>Internet Explorer 11 on Windows 7 for x64-based Systems Service Pack 1</b></td>
</tr>
</thead>
<tr>
<td><b>CVE ID</b></td>
<td><b>KB Article</b></td>
<td><b>Restart Required</b></td>
<td><b>Severity</b></td>
<td><b>Impact</b></td>
</tr>
<tr>
<td>CVE-2016-0154</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=3148198; URL=https://www.microsoft.com/downloads/details.aspx?familyid=84b9ce93-9123-4f06-8ae1-fcb221a41938; SubType=Security Update}">@{ID=3148198; URL=https://www.microsoft.com/downloads/details.aspx?familyid=84b9ce93-9123-4f06-8ae1-fcb221a41938; SubType=Security Update}</a><br></td>
<td>Yes<br></td>
<td>Critical<br></td>
<td>Remote Code Execution<br></td>
</tr> <tr>
<td>CVE-2016-0162</td>
<td><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=4015549; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4015549; SubType=Monthly Rollup}">@{ID=4015549; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4015549; SubType=Monthly Rollup}</a><br><br /><a href="https://catalog.update.microsoft.com/v7/site/Search.aspx?q=@{ID=4014661; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4014661; SubType=IE Cumulative}">@{ID=4014661; URL=https://catalog.update.microsoft.com/v7/site/Search.aspx?q=KB4014661; SubType=IE Cumulative}</a><br></td>
<td>Yes<br><br />Yes<br></td>
<td>Moderate<br></td>
<td>Information Disclosure<br></td>
</tr>
</table>

最佳答案

你可以试试这个

from bs4 import BeautifulSoup as soup

s = soup(open('april12016.html').read(), 'lxml')

links = []
head = {}

for table in s.find_all('table', attrs={'class':'affected_software'}):
heading = table.select('b')[0].text
links = []
for a in table.find_all('a'):
links.append(a.get('href')) # get all the links
head[heading] = links # put it in a dictionay

关于python - 尝试从 html 表 python 添加某个值作为键并为该键添加多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50377524/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com