【发布时间】:2015-03-04 18:36:37
【问题描述】:
我是第一次使用 Beautiful Soup,我正在尝试获取网页中特定元素的值。
例如在这段代码中sn-p:
<div class="otg-vendor-name"><a class="otg-vendor-name-link" href="http://www.3brotherskitchen.com" target="_blank">3 Brothers Kitchen</a></div>
我希望从 .
到目前为止,我尝试了一些似乎不起作用的方法:
import urllib2
from bs4 import BeautifulSoup
url = "http://someurl"
def get_all_vendors():
try:
web_page = urllib2.urlopen(url).read()
soup = BeautifulSoup(web_page)
c = []
c.append(soup.findAll("div", {"class":'otg-vendor-name'}).contents)
print c
except urllib2.HTTPError:
print("HTTPERROR!")
except urllib2.URLError:
print("URLERROR!")
return c
【问题讨论】:
标签: beautifulsoup html-parsing