oldtroy 2022-06-29 19:23 采纳率: 84.6%
浏览 148
已结题

BeautifulSoup4获取select标签的当前选项

我需要用bs4获取一个select标签中被选中的option元素的value。我知道用selenium可以轻松做到 driver.find_element(By.ID, 'sex').get_attribute('value'), 但是selenium直接读取速度太慢,我的数据量庞大,所以不能用。

<!DOCTYPE html>  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
        <meta name="content-type" content="text/html; charset=UTF-8">  
        <title>Hello World</title>  
    </head>  
<body>  
    <div>
        <label>Info:</label>
        <select title="This is an info Component" id="sex" name="sex" class="ComboBox">
            <option value="">--Select--</option>
            <option value="M">Male</option>
            <option value="F">Female</option>
            <option value="O">Others</option>
        </select>
    </div>
</body>  
</html>  

我尝试了一下,无论选了哪个option,HTML都没变化,直接分析HTML元素,我分辨不出哪个option被选中了,但是在Edge控制台上,用 document.querySelector('#sex option:checked') 可以直接返回被选中的元素。于是我参照这个方法写了下面这段代码:

from bs4 import BeautifulSoup as bs
from selenium import webdriver

wb = webdriver.Edge()
wb.get('xxxxxxxx')
soup = bs(wb.page_source, 'html.parser')
elements = soup.select('#sex option:checked')
for element in elements:
    print(element)

代码可以运行,但是结果为空,并没有找到相应的option元素。
我想问通过bs4,有什么办法可以返回select标签被选中option的值吗

  • 写回答

1条回答 默认 最新

  • 天际的海浪 2022-06-30 01:32
    关注

    select标签中的option元素是页面加载时就被选中的吗
    如果是页面加载之后才被选中的,需要在option元素被选中之后再获取wb.page_source并执行soup = bs(wb.page_source, 'html.parser')
    另外因为bs4只是分析页面的源代码,
    如果页面中是用document.getElementById("sex").value = "F";方式让option元素被选中的, 源代码中没有变化,也就没办法用bs4分析了

    如果页面中是用document.getElementById("sex").options[2].setAttribute("selected","selected");方式让option元素被选中的, 源代码中才有变化,才能用bs4分析

    <!DOCTYPE html>  
    <html>  
        <head>  
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
            <meta name="content-type" content="text/html; charset=UTF-8">  
            <title>Hello World</title>  
        </head>  
    <body>  
        <div>
            <label>Info:</label>
            <select title="This is an info Component" id="sex" name="sex" class="ComboBox">
                <option value="">--Select--</option>
                <option value="M">Male</option>
                <option value="F">Female</option>
                <option value="O">Others</option>
            </select>
        </div>
        
    <script type="text/javascript">
    setTimeout(function(){
    document.getElementById("sex").options[2].setAttribute("selected","selected");
    console.log(document.body.innerHTML);
    }, 1000);
    </script>    
    </body>  
    </html>  
    
    from bs4 import BeautifulSoup as bs
    from selenium import webdriver
    import time
    wb = webdriver.Edge()
    wb.get('file:///E:/Documents/aaa.html')
    time.sleep(5)
    print(wb.page_source)
    soup = bs(wb.page_source, 'html.parser')
    elements = soup.select('#sex option:checked')
    for element in elements:
        print(element)
    

    img

    对于用document.getElementById("sex").value = "F";方式让option元素被选中的,唯一的方法只能是用 selenium 为选中的option元素再添加上selected属性让 源代码中有变化,再用bs4分析. 但这等同直接用selenium 处理了,没有意义了.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 7月8日
  • 已采纳回答 6月30日
  • 赞助了问题酬金10元 6月29日
  • 修改了问题 6月29日
  • 展开全部

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?