如何解决此问题中的错误?

时间:2019-05-30 17:35:29

标签: python pandas web-scraping jupyter-notebook

程序正在运行,但是我得到一个空列表,并且数据框到excel的转换不起作用。

from bs4 import BeautifulSoup
import requests

l=[]
base_url="https://www.realtor.com/realestateandhomes-search/Sherman- 
          Oaks_CA/pg-"
for page in range(1,13,1):
    print(base_url+str(page))
    r_1=requests.get(base_url+str(page))
    soup=BeautifulSoup(r_1.content)
    all=soup.find_all("div",{"class":"card-box js-navigate-to js-record- 
        user-activity"})
    for item in all:
        d={}
        try:
            d["Address"]=item.find("span",{"class":"listing-street- 
                         address"}).text.replace(" ","").replace("\n","")
        except:
            d["Address"]=None

        try:
            d["Locality"]=item.find("span",{"class":"listing- 
                          city"}).text+" "+item.find("span", 
                          {"class":"listing-region"}).text+" 
                          "+item.find("span",{"class":"listing- 
                          postal"}).text
        except:
            d["Locality"]=None

        try:
            d["Price"]=item.find("span",{"class":"data-price"}).text
        except:
            d["Price"]=None

        try:
            d["Beds"]=item.find("span",{"class":"data-value meta- 
                      beds"}).text
        except:
            d["Beds"]=None

        try:
            d["Baths"]=item.find("li",{"data-label":"property-meta- 
                       baths"}).find("span",{"class":"data-value"}).text
        except:
            d["Baths"]=None

        try:
            d["Area"]=item.find("li",{"data-label":"property-meta- 
                      sqft"}).text
        except:
            d["Area"]=None

        try:
            d["Lot Size"]=item.find("li",{"data-label":"property-meta- 
                          lotsize"}).text
        except:
            d["Lot Size"]=None
        print(" ")
        l.append(d)

import pandas as pd
df=pd.DataFrame(l)

df.to_csv("Real_Estate_list.csv")
df.to_excel("Real_Estate_list.xlsx")

我应该得到一个包含所有值的表,但是得到一个空列表。

至于df.to_excel(“ Real_Estate_list.xlsx”)我得到: ModuleNotFoundError:没有名为“ openpyxl”的模块

0 个答案:

没有答案