【问题标题】:Python Pandas : Getting only 3 first elements from tablePython Pandas:从表中仅获取 3 个第一个元素
【发布时间】:2021-11-08 20:41:58
【问题描述】:
我使用 pandas 来抓取这个网站 https://www.mapsofworld.com/lat_long/poland-lat-long.html 但我只获得了 3 个元素。如何从表格中获取所有元素?
import numpy as np
import pandas as pd
#for getting world map
import folium
# Retreiving Latitude and Longitude coordinates
info = pd.read_html("https://www.mapsofworld.com/lat_long/poland-lat-long.html",match='Augustow',skiprows=2)
#convering the table data into DataFrame
coordinates = pd.DataFrame(info[0])
data = coordinates.head()
print(data)
【问题讨论】:
标签:
python
pandas
web-scraping
【解决方案1】:
看起来如果您安装并use html5lib as your parser 它可能会解决您的问题:
df = pd.read_html("https://www.mapsofworld.com/lat_long/poland-lat-long.html",attrs={"class":"tableizer-table"},skiprows=2,flavor="html5lib")
>>>df
[ 0 1 2
0 Locations Latitude Longitude
1 NaN NaN NaN
2 Augustow 53°51'N 23°00'E
3 Auschwitz/Oswiecim 50°02'N 19°11'E
4 Biala Podxlaska 52°04'N 23°06'E
.. ... ... ...
177 Zawiercie 50°30'N 19°24'E
178 Zdunska Wola 51°37'N 18°59'E
179 Zgorzelec 51°10'N 15°0'E
180 Zyrardow 52°3'N 20°28'E
181 Zywiec 49°42'N 19°10'E
[182 rows x 3 columns]]