使用Python&BeautifulSoup废弃HTML标记标识符值

2024-06-23 02:41:54 发布

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

我仍然在学习Python,并且一直在使用beauthoulsoup来获取一些web数据,我的问题是:是否可以刮取标签ID值?在

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

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

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

谢谢你的指点!在


Tags: 数据代码标记webidhtml标签class
2条回答

请看我发布的评论中的链接,但我认为你想做的是这样的

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

如果你要发布一个你的代码的例子,我可以定制这个,但你没有,它是相当通用的。在

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

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

。。。在

^{pr2}$

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

相关问题 更多 >