我使用requests和beautiful soup提取一些wikipedia数据,然后我想将其放入pandas数据框(所有单元格的列名为column1)。我在尝试调用 Dataframe 时遇到此错误:
valueerror:未正确调用 Dataframe 构造函数!
有人有什么建议吗?这是我的密码:
import pandas as pd
import requests
import bs4
result = requests.get("https://en.wikipedia.org/wiki/United_States")
# Put the data into BS
soup = bs4.BeautifulSoup(result.text,"lxml")
# select any soup element
soup.select('p')
# take the result from the list element
Results = soup.select('p')[2].getText()
# print to csv
df = pd.DataFrame(Results, columns=['Column1'])
print(df)
1条答案
按热度按时间efzxgjgh1#
结果是一个文本字符串:
你可以使用
io
```pd.DataFrame(io.StringIO(Results), columns=['Column1'])
pd.DataFrame([Results], columns=['Column1'])
Column1
0 The United States of America (U.S.A. or USA), commonly known as the United States (U.S. or US) or America, is a country primarily located in North America. It consists of 50 states, a federal district, five major unincorporated territories, 326 Indian reservations, and some minor possessions.[g] At 3.8 million square miles (9.8 million square kilometers), it is the world's third- or fourth-largest country by total area.[c] It borders Canada to the north and Mexico to the south. With a population of more than 328.2 million people, it is the third most populous country in the world. The national capital is Washington, D.C., and the most populous city is New York City.\n