我很难用“靓汤”将这个维基百科列表与洛杉矶的邻居们一块儿删除。我得到了身体的所有内容,而不仅仅是我想要的邻里名单。我看到了很多关于如何刮表的内容,但是我在如何在这种情况下应用表逻辑方面被绊住了。 这是我一直在使用的代码:
import BeautifulSoup
address = 'Los Angeles, United States'
url = "https://en.wikipedia.org/wiki/List_of_districts_and_neighborhoods_of_Los_Angeles"
source = requests.get(url).text
soup = BeautifulSoup(source,'lxml')
neighborhoodList = []
-- append the data into the list
for row in soup.find_all("div", class_="mw-body")[0].findAll("li"):
neighborhoodList.append(row.text.replace(', LA',''))
df_neighborhood = pd.DataFrame({"Neighborhood": neighborhoodList})
如果查看页面源,则邻域条目位于具有“div col”类的div中,并且链接包含“title”属性
此外,在追加过程中,似乎不需要替换文本
以下代码:
结果:
相关问题 更多 >
编程相关推荐