使用Python&BeautifulSoup刮取HTML标记标识符值

2024-06-23 02:42:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我仍在学习Python,并一直在使用BeautifulSoup删除一些web数据,我的问题是:是否可以删除标记ID值

也许最好举个例子,我正在使用的HTML代码如下所示:

<A CLASS="someClass" uniqueID="someValue" anotherID="someOtherValue">
Here is the data I can scrape right now.
</A>

因此,从上面的示例中,我可以成功地刮取A标记之间的内容,但我不知道如何获取A标记中存在的中的“uniqueID”和“anotherID”的值

谢谢你的指点


Tags: 数据代码标记webidherehtmlclass
2条回答

要获取elementattributes,可以使用.get()方法(python3),即:

<A CLASS="someClass" uniqueID="someValue" anotherID="someOtherValue">
Here is the data I can scrape right now.
</A>

_as = xmlSoup.find_all('a')

for a in _as :
    print(a.get('CLASS'))
    print(a.get('uniqueID'))
    print(a.get('anotherID'))
    print(a.text))

上面将循环html中的所有a标记,并打印每个标记的指定属性

请看我贴出的评论中的链接,但我认为你是在尝试这样做

soup.find("a", {"uniqueID": "someValue"})

如果你要发布一个代码示例,我可以对其进行裁剪,但由于你没有这样做,这是相当通用的

相关问题 更多 >