gpt4 book ai didi

用于HTML解析的Python正则表达式(BeautifulSoup)

转载 作者:太空狗 更新时间:2023-10-29 17:51:45 28 4
gpt4 key购买 nike

我想获取 HTML 中隐藏输入字段的值。

<input type="hidden" name="fooId" value="12-3456789-1111111111" />

我想用 Python 编写一个正则表达式,它将返回 fooId 的值,前提是我知道 HTML 中的行遵循以下格式

<input type="hidden" name="fooId" value="**[id is here]**" />

有人可以提供一个 Python 示例来解析值的 HTML 吗?

最佳答案

对于这种特殊情况,BeautifulSoup 比正则表达式更难编写,但它更健壮......我只是为 BeautifulSoup 示例做出贡献,因为你已经知道要使用哪个正则表达式:-)

from BeautifulSoup import BeautifulSoup

#Or retrieve it from the web, etc.
html_data = open('/yourwebsite/page.html','r').read()

#Create the soup object from the HTML data
soup = BeautifulSoup(html_data)
fooId = soup.find('input',name='fooId',type='hidden') #Find the proper tag
value = fooId.attrs[2][1] #The value of the third attribute of the desired tag
#or index it directly via fooId['value']

关于用于HTML解析的Python正则表达式(BeautifulSoup),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55391/

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