Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Beautiful Soup, how to retrieve the 'data-id'

Im trying to figure out how i would extract the data-id from something like

<div class="tweet-text" data-id="1091362563202396165">
 dir="ltr" href="h" rel="nofollow noopener" target="_blank" title=.html">cnbc.com/2019/02/01/non…</a>
</div> 

I've tried select('.data-id') also d.get_text('data-id') , d.match('data-id'). I'm trying to extract the actual value in data-id, the number.... Thanks for your help. I'll keep trying in the mean time...

like image 302
chris clarke Avatar asked Oct 21 '25 11:10

chris clarke


1 Answers

You can use a search query of attrs={"data-id": True} to find elements that have a value for data-id.

for item in soupdata.find_all(attrs={"data-id": True}):
    print (item['data-id'])
like image 57
John Gordon Avatar answered Oct 23 '25 01:10

John Gordon