【问题标题】:Why is my beautifulSoup code coming up with an empty data frame?为什么我的 beautifulSoup 代码会出现一个空数据框?
【发布时间】:2020-03-08 16:33:16
【问题描述】:

我正在尝试在维基百科页面上抓取表格,但我的 BeautifulSoup 代码无法正常工作 - 它一直以空数据框的形式出现。有什么建议吗?

import requests
import pandas as pd
from bs4 import BeautifulSoup
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'}
url = "https://en.wikipedia.org/wiki/List_of_postal_codes_of_Canada:_M"
r = requests.get(url,headers=headers)

soup = BeautifulSoup(r.content,"html.parser")
table = soup.find_all('table')[1]
rows = table.find_all('tr')
row_list = list()

for tr in rows:
    td = tr.find_all('td')
    row = [i.text for i in td]
    row_list.append(row)
df_bs = pd.DataFrame(row_list,columns=['PostalCode','Borough','Neighborhood'])
df_bs.set_index('PostalCode',inplace=True)
print(df_bs)

【问题讨论】:

    标签: python pandas beautifulsoup


    【解决方案1】:

    您正在查看错误的表格。更改此行:

    table = soup.find_all('table')[1]
    

    到这里:

    table = soup.find('table')
    

    【讨论】:

      猜你喜欢
      • 2014-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      相关资源
      最近更新 更多