程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了获取数组中单选按钮的内容大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决获取数组中单选按钮的内容?

开发过程中遇到获取数组中单选按钮的内容的问题如何解决?下面主要结合日常开发的经验,给出你关于获取数组中单选按钮的内容的解决方法建议,希望对你解决获取数组中单选按钮的内容有所启发或帮助;

我有以下来自此 website 的单选按钮列表:

@H_874_4@<div class="map-filters"> <div class="map-filter-radio radio"> <label> <input type="radio" name="adoptionStatus" alt="IFRS Standards are required for use by all or most domestic publicly accountable entitIEs." title="IFRS Standards are required for use by all or most domestic publicly accountable entitIEs." value="97f9b22998d546f7856bb1b4f0586521"> IFRS Standards are required for domestic public companIEs </label> </div> <div class="map-filter-radio radio"> <label> <input type="radio" name="adoptionStatus" alt="IFRS Standards are permitted,but not required,for use by at least some domestic publicly accountable entitIEs,including Listed companIEs and financial institutions." title="IFRS Standards are permitted,including Listed companIEs and financial institutions." value="cae64c6b731d47cca7565b2a74d11d53"> IFRS Standards are permitted but not required for domestic public companIEs </label> </div> </div>

我想获取标签标签中的文本和输入类型单选的值,如下所示:

@H_874_4@Key Value 97f9b22998d546f7856bb1b4f0586521 IFRS Standards are required for domestic public companIEs cae64c6b731d47cca7565b2a74d11d53 IFRS Standards are permitted but not required for domestic public companIEs

这是我的工作:

@H_874_4@session = HTMLSession() resp = session.get("https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisDiction/") resp.HTMl.render() soup = BeautifulSoup(resp.HTMl.HTML,"lxml") option_Tags = soup.find("input",{"name":"adoptionStatus"}) filters = [tag.text for tag in option_Tags] print(filters)

解决方法

试试这个:

@H_874_4@import requests from bs4 import BeautifulSoup url = "https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisDiction/" page_html = requests.get(url).text soup = BeautifulSoup(page_html,"lxml").find_all("input",{"type": "radio"}) buttons = [[button["alt"],button["value"]] for button in soup] for button in buttons: text,value = button print(f"Text: {text}\nValue: {value}")

输出:

@H_874_4@Text: IFRS Standards are required for use by all or most domestic publicly accountable entities. Value: 97f9b22998d546f7856bb1b4f0586521 Text: IFRS Standards are permitted,but not required,for use by at least some domestic publicly accountable entities,including listed companies and financial institutions. Value: cae64c6b731d47cca7565b2a74d11d53 Text: IFRS Standards are required or permitted for use by foreign securities issuers. Value: 3adc18f07ff64c908a6d835e08344531 Text: In most cases an SME may also choose full IFRS Standards. In some cases,an SME may also choose local standards for SMEs. Value: 665a976cd22f4b2db99c57e2ab98e633 Text: The body with authority to adopt financial reporTing standards is actively studying whether to adopt the <em>IFRS for SMEs</em> Standard. Value: ff784361818644798ea899f81b8b6d61 ,

您可以通过访问 key HTML 属性来获取 value。并使用 next_sibling 方法访问 value

@H_874_4@from bs4 import BeautifulSoup from requests_html import HTMLSession session = HTMLSession() resp = session.get( "https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisDiction/" ) resp.html.render() soup = BeautifulSoup(resp.html.html,"lxml") fmt_String = "{:<40} {:<20}" print(fmt_String.format("Key","Value")) for tag in soup.find_all("input",{"name": "adoptionStatus"}): print(fmt_String.format(tag["value"],tag.next_sibling.Strip()))

输出:

@H_874_4@Key Value 97f9b22998d546f7856bb1b4f0586521 IFRS Standards are required for domestic public companies cae64c6b731d47cca7565b2a74d11d53 IFRS Standards are permitted but not required for domestic public companies 3adc18f07ff64c908a6d835e08344531 IFRS Standards are required or permitted for lisTings by foreign companies 665a976cd22f4b2db99c57e2ab98e633 The IFRS for SMEs Standard is required or permitted ff784361818644798ea899f81b8b6d61 The IFRS for SMEs Standard is under consideration , @H_874_4@value_tags = soup.find("div",class_="map-filter-radio radio").text.Strip() key_tag = option_tags.attrs["value"]

这是第一个元素

,

如果没有全文版本,而不是更短的版本,您可以使用css和类选择器进行更快的匹配

@H_874_4@import requests from bs4 import BeautifulSoup as bs soup = bs(requests.get('https://www.ifrs.org/use-around-the-world/use-of-ifrs-standards-by-jurisDiction/').text,'lxml') items = {I['id']:i.text for i in soup.SELEct('.status-text')} print(items)

大佬总结

以上是大佬教程为你收集整理的获取数组中单选按钮的内容全部内容,希望文章能够帮你解决获取数组中单选按钮的内容所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: