Python 如何在使用beautifulsoup解析ID时从包含ID的HTML标记返回ID
我想将html源代码中的ID写入CSV文件,但很难找到合适的代码 我想解决两个问题 案例1) 基本上,我想保留标签中的“F1”,这是它的ID,并在文件中用文本写入它 案例2) 我很难找到合适的代码将其写入文件 不幸的是,我没有准备好的代码 如果您能为我提供一种解决其中任何一个问题的方法,那将非常有帮助。很快Python 如何在使用beautifulsoup解析ID时从包含ID的HTML标记返回ID,python,beautifulsoup,Python,Beautifulsoup,我想将html源代码中的ID写入CSV文件,但很难找到合适的代码 我想解决两个问题 案例1) 基本上,我想保留标签中的“F1”,这是它的ID,并在文件中用文本写入它 案例2) 我很难找到合适的代码将其写入文件 不幸的是,我没有准备好的代码 如果您能为我提供一种解决其中任何一个问题的方法,那将非常有帮助。很快 soup.find('footnote').get('id') 示例代码: from bs4 import BeautifulSoup as BS text = '''<foo
soup.find('footnote').get('id')
示例代码:
from bs4 import BeautifulSoup as BS
text = '''<footnotes>
<footnote id="F1">Includes 4,675.96 restricted stock units that will vest and settle in shares of the Company's common stock on a one-for-one basis on February 23, 2012.</footnote>
</footnotes>
<exerciseDate>
<footnoteId id="F5"/>
</exerciseDate>'''
soup = BS(text, 'html.parser')
item = soup.find('footnote')
print(item.get('id'), item.get_text())
item = soup.find('footnoteid')
print(item.get('id'))
从bs4导入美化组作为BS
文本='''
包括4675.96个限制性股票单位,将于2012年2月23日以一对一的方式授予和结算公司普通股的股份。
'''
soup=BS(文本“html.parser”)
item=soup.find('footnote')
打印(item.get('id'),item.get_text())
item=soup.find('footnoteid')
打印(item.get('id'))
以下是一个简短的示例代码,让您开始学习:
from bs4 import BeautifulSoup
html_text = """
<footnotes>
<footnote id="F1">Includes 4,675.96 restricted stock units that will vest and settle in shares of the Company's common stock on a one-for-one basis on February 23, 2012.</footnote>
</footnotes>
"""
# ~~ Parse HTML ~~ #
soup = BeautifulSoup(html_text,'html.parser')
# ~~ Find footnote tags in the html ~~ #
footnote_tag = soup.find("footnote")
# From footnote tag, get id
footnote_id = footnote_tag['id']
# From footnote tag, get text
footnote_text = footnote_tag.get_text()
# Putting id with text
return_statement = "ID {0} {1}".format(footnote_id,footnote_text)
从bs4导入美化组
html_text=“”
包括4675.96个限制性股票单位,将于2012年2月23日以一对一的方式授予和结算公司普通股的股份。
"""
#~~解析HTML~~#
soup=BeautifulSoup(html_文本,'html.parser')
#~~在html中查找脚注标记~#
footnote_tag=soup.find(“footnote”)
#从脚注标记中,获取id
footnote\u id=footnote\u标签['id']
#从脚注标记获取文本
footnote\u text=footnote\u tag.get\u text()
#将id与文本放在一起
return_statement=“ID{0}{1}”。格式(footnote_ID,footnote_text)
显示您的代码。您应该在页面上只问一个问题。soup.find('footnote')。get('id')
F5
soup.find('footnote').get('id')
from bs4 import BeautifulSoup as BS
text = '''<footnotes>
<footnote id="F1">Includes 4,675.96 restricted stock units that will vest and settle in shares of the Company's common stock on a one-for-one basis on February 23, 2012.</footnote>
</footnotes>
<exerciseDate>
<footnoteId id="F5"/>
</exerciseDate>'''
soup = BS(text, 'html.parser')
item = soup.find('footnote')
print(item.get('id'), item.get_text())
item = soup.find('footnoteid')
print(item.get('id'))
from bs4 import BeautifulSoup
html_text = """
<footnotes>
<footnote id="F1">Includes 4,675.96 restricted stock units that will vest and settle in shares of the Company's common stock on a one-for-one basis on February 23, 2012.</footnote>
</footnotes>
"""
# ~~ Parse HTML ~~ #
soup = BeautifulSoup(html_text,'html.parser')
# ~~ Find footnote tags in the html ~~ #
footnote_tag = soup.find("footnote")
# From footnote tag, get id
footnote_id = footnote_tag['id']
# From footnote tag, get text
footnote_text = footnote_tag.get_text()
# Putting id with text
return_statement = "ID {0} {1}".format(footnote_id,footnote_text)