【发布时间】:2021-03-23 08:18:16
【问题描述】:
我正在尝试从位于 Wikipedia https://en.wikipedia.org/wiki/Districts_of_Warsaw 页面上的 Localities 表中获取数据。
我想收集这些数据并将其放入包含两列 ["Districts"] 和 ["Neighbourhoods"] 的数据框中。
到目前为止,我的代码如下所示:
url = "https://en.wikipedia.org/wiki/Districts_of_Warsaw"
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page, "html")
table = soup.find_all('table')[2]
A=[]
B=[]
for row in table.findAll('tr'):
cells=row.findAll('td')
if len(cells)==2:
A.append(cells[0].find(text=True))
B.append(cells[1].find(text=True))
df=pd.DataFrame(A,columns=['Neighbourhood'])
df['District']=B
print(df)
这给出了以下数据框:
当然,抓取 Neighborhood 列是不正确的,因为它们包含在列表中,但我不知道应该怎么做,所以很高兴有任何提示。
除此之外,我会感谢任何提示,为什么抓取只给我 10 个区而不是 18 个区。
【问题讨论】:
标签: python dataframe web-scraping beautifulsoup