Beautiful Soup + Pandas仅输出CSV的最后一项

时间:2020-09-13 08:03:17

标签: python python-3.x pandas dataframe beautifulsoup

奇怪!我在Beautiful Soup 4中使用PandasPython 3.8。我正在尝试使用requests从网页上抓取数据。我的代码在首页上打印了所有属性,但无法将所有属性都写到CSV文件中,而是只写了最后一项。我在这里想念什么?下面的代码。

listofProperties = []
for property in properties:
    pdFrame = {}
    pdFrame["House Price"] = property.find("a", class_ = "text-price").text.replace("\n", "").replace("Offers over", "").replace(" ", "")
    pdFrame["House Address"] = property.find("a", class_ = "listing-results-address").text
    pdFrame["Number of Beds"] = property.find("span", class_ = "num-beds")['title']
    pdFrame["Number of Baths"] = property.find("span", class_ = "num-baths")['title']
    pdFrame["Number of Reception Rooms"] = property.find("span", class_ = "num-reception")['title']
    pdFrame["Sold By"] = property.find("p", class_ = "listing-results-marketed").find("span").text
    propertyArea = random.randint(300,2200)
    propertyArea = str(propertyArea)
    pdFrame["House Size"] = propertyArea + " sq. ft"
    pdFrame["Agent Phone"] = property.find("span", class_ = "agent_phone").text.replace(" **", "").replace("\n", "")

listofProperties.append(pdFrame)
print(len(listofProperties))
print(listofProperties)
df = pandas.DataFrame(listofProperties)
df.to_csv("scrappedProperties.csv", index=False)

任何帮助表示赞赏。 谢谢!

1 个答案:

答案 0 :(得分:0)

我在代码中犯了一个小错误,我在循环外称这个listofProperties.append(pdFrame)。 @sushanth指出了这一点。

谢谢!