3
我是新来BeautifulSoup并试图从以下网站数据: http://www.expatistan.com/cost-of-living/comparison/phoenix/new-york-city的Python - 网页抓取 - BeautifulSoup
我试图把解压出来的总结百分比每个类别(食品,住房,衣服,交通,个人护理和娱乐)。因此,对于上面提供的链接,我想提取百分比:48%,129%,63%,43%,42%,42%和72%。
不幸的是,我使用BeautifulSoup的当前Python代码会提取出以下百分比:12%,85%,63%,21%,42%和48%。我不知道为什么会这样。任何帮助在这里将不胜感激!这里是我的代码:
import urllib2
from bs4 import BeautifulSoup
url = "http://www.expatistan.com/cost-of-living/comparison/phoenix/new-york-city"
page = urllib2.urlopen(url)
soup_expatistan = BeautifulSoup(page)
page.close()
expatistan_table = soup_expatistan.find("table",class_="comparison")
expatistan_titles = expatistan_table.find_all("tr",class_="expandable")
for expatistan_title in expatistan_titles:
published_date = expatistan_title.find("th",class_="percent")
print(published_date.span.string)
非常感谢您的帮助! – user3599514