如何在Python中使用Beautiful Soup来刮掉它?

时间:2012-06-03 15:56:37

标签: 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页面并获取如下数据:

link = http://www.chrisstucchio.com/blog/2012/bandit_algorithms_vs_ab.html
text = Why Multi-armed Bandit algorithms are superior to A/B testing (with Math)
user_id = yummyfajitas
item_id = 4060658

1 个答案:

答案 0 :(得分:1)

如果每次都按照完全相同的顺序:

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