如何使用Python中的Beautiful Soup来抓取?

2024-06-16 12:54:06 发布

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

<a href="http://www.chrisstucchio.com/blog/2012/bandit_algorithms_vs_ab.html">Why Multi-armed Bandit algorithms are superior to A/B testing (with Math)</a>, <a href="user?id=yummyfajitas">yummyfajitas</a>, <a href="item?id=4060658">11 comments</a>, 

如何在一个以上面写的html为内容的html页面中抓取数据,如下所示:

^{pr2}$

Tags: comidhttpabhtmlwwwblogmulti
1条回答
网友
1楼 · 发布于 2024-06-16 12:54:06

如果每次的顺序都是一样的:

html = r'<a href="http://www.chrisstucchio.com/blog/2012/bandit_algorithms_vs_ab.html">Why Multi-armed Bandit algorithms are superior to A/B testing (with Math)</a>, <a href="user?id=yummyfajitas">yummyfajitas</a>, <a href="item?id=4060658">11 comments</a>, '

from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(html) #sort the html
bowl = soup.findAll('a') #find all links in the html

link = bowl[0]['href'] #find the first 'a' tags href
text = bowl[0].contents[0] #find the first tags url
user_id = bowl[1]['href'].split('?id=')[1] #split on '?id=' and take the second  value. could be [-1] too
item_id = bowl[2]['href'].split('?id=')[1]

print 'link:', link
print 'text:', text
print 'user_id:', user_id
print 'item_id:', item_id

相关问题 更多 >