使用 Beautiful Soup 解析 html 表单输入标签

用户7400738

我想放弃一个网站。如果只有一个打开和一个关闭表单标签和数据在这两者之间没有问题。但是当网站上的数据显示在复选框下时,则代码中的数据处于奇怪的位置。有人有同样的问题吗?

这是我想要数据的基本示例网页:

<div class="label"></div>
<input disabled="" type="checkbox" name="t_pow_ports:f_p_a:3486" class="forminput" id="ajaxField-76" checked="">
&nbsp;&nbsp;Airport
<div class="label"></div>
<input disabled="" type="checkbox" name="t_pow_ports:f_p_b:3486" checked="" class="forminput" id="ajaxField-77">
&nbsp;&nbsp;Bunkers
<div class="label"></div>
<input disabled="" type="checkbox" name="t_pow_ports:f_p_c:3486" class="forminput" id="ajaxField-78">
&nbsp;&nbsp;Containers
<div class="label"></div>
<input disabled="" type="checkbox" name="t_pow_ports:f_p_l:3486" class="forminput" id="ajaxField-79">
&nbsp;&nbsp;Cruise
<div class="label"></div>
....

我需要获取数据:Airport、Bunkers 等(数据),它们的输入数组中有“已检查 =""”。第一个问题:确保我只得到检查值第二个问题:如何获取介于两者之间的数据

<div>..</div><input...> data <div>...</div> 

通过使用以下代码:

import requests
import bs4
from bs4 import BeautifulSoup
import pandas

r = requests.get("http://directories.lloydslist.com/?p=1635")
c = r.content 
soup = BeautifulSoup(c, "html.parser")
print(soup.prettify())
all = soup.find_all("div",{"id":"section-1785-body"},{"class":"sectionbody"})

我得到以下格式:

<div class="label"></div>
<input checked="" class="forminput" disabled="" id="ajaxField-115"   name="t_pow_ports:f_p_a:5779" type="checkbox"/>  
Airport
<div class="label"></div>
<input checked="" class="forminput" disabled="" id="ajaxField-116" name="t_pow_ports:f_p_b:5779" type="checkbox"/>  
Bunkers
<div class="label"></div>
.....
....
<input checked="" class="forminput" disabled="" id="ajaxField-119"      name="t_pow_ports:f_p_y:5779" type="checkbox"/>  Dry Bulk
<div class="label"></div></div>

因此,如果我使用以下代码:

abc = all[0].find_all("input", {"class":"forminput"},"checked")

我没有得到任何数据:

<input class="forminput" disabled="" id="ajaxField-20"    name="t_pow_ports:f_p_a:595" type="checkbox"/>,
<input class="forminput" disabled="" id="ajaxField-21" name="t_pow_ports:f_p_b:595" type="checkbox"/>,
 <input class="forminput" disabled="" id="ajaxField-22" name="t_pow_ports:f_p_c:595" type="checkbox"/>,
....

有谁知道解决这个问题的方法?

克里斯托罂粟花

您需要使用navigableString在检查输入后获取下一个兄弟。

请尝试以下方法:

from bs4 import BeautifulSoup as Soup

html_str = """
<div>
    <div class="label"></div>
    <input disabled="" type="checkbox" name="t_pow_ports:f_p_a:3486" class="forminput" id="ajaxField-76" checked=""/>
    &nbsp;&nbsp;Airport

    <div class="label"></div>
    <input disabled="" type="checkbox" name="t_pow_ports:f_p_b:3486" checked="" class="forminput" id="ajaxField-77"/>
    &nbsp;&nbsp;Bunkers

    <div class="label"></div>
    <input disabled="" type="checkbox" name="t_pow_ports:f_p_c:3486" class="forminput" id="ajaxField-78"/>
    &nbsp;&nbsp;Containers

    <div class="label"></div>
    <input disabled="" type="checkbox" name="t_pow_ports:f_p_l:3486" class="forminput" id="ajaxField-79"/>
    &nbsp;&nbsp;Cruise

    <div class="label"></div>
</div>
"""

soup = Soup(html_str, "html.parser")

forminput = soup.find_all("input", {"class":"forminput"})
for item in forminput:
    if item.get('checked') is not None:
        # now work with navigable string! be careful for empty lines
        name = item.next_sibling.strip()
        print(name)

此代码段的输出是:

Airport
Bunkers

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在python中使用Beautiful Soup解析html

Beautiful Soup 解析包含 JSON 的 HTML

如何使用scrapy或beautiful Soup提取特定html标签的内容?

使用Beautiful Soup获取所有HTML标签

Python:使用 Beautiful Soup 从 HTML 标签中提取图像源

使用 Beautiful Soup 解析 XML 的问题

使用Beautiful Soup在Python中解析网站

用Beautiful Soup解析HTML。从特定标签返回文本

如何使用Beautiful Soup从HTML获取文本

如何使用Beautiful Soup删除html注释

如何使用Beautiful Soup来<script>标签?

用Beautiful Soup解析HTML以在href后获得链接

当有多个相似标签时,使用 Beautiful Soup 从特定 HTML 标签中提取文本

Beautiful Soup 没有解析所有标签

使用Beautiful Soup解析NELL Knowledgele Base页面

如何单击/使用从 Python 中的 Beautiful Soup 解析的链接

使用 Beautiful Soup (bs4) 解析和修改内容

如何使用python beautiful soup从HTML下面获取标签和ID信息

如何使用Python和Beautiful Soup修复html列表片段中缺少的ul标签

是否可以使用Beautiful Soup以编程方式组合某些HTML标签的内容?

如何使用Beautiful Soup在html页面中的两个标签之间获取内容?

使用 Beautiful Soup Python 在 HTML 代码的 'img' 标签内打印 'id' 值

用 Beautiful Soup 解析 KML

Beautiful Soup 从 JSON 解析值

Python3 Beautiful Soup获取HTML标签锚

如何使用Python Beautiful Soup获取html的标记名称?

如何使用Beautiful Soup从HTML提取特定的脚本元素

在HTML表中使用Beautiful soup查找信息

使用页面文本通过“ Beautiful Soup”选择“ html”元素