2017-01-15 1 views
0

私の最初のウェブサイトを見つけようとしています。私は異常なエラーで死んだと思う。Python Web Scraping:NDFrame以外のオブジェクトを連結できません

import requests 
import pandas as pd 
from bs4 import BeautifulSoup 

url="http://property.sulekha.com/apartments-flats-in-pimpri-chinchwad-general-pune-for-sale" 
r = requests.get(url) 
soup = BeautifulSoup(r.content) 

g_data = soup.find_all("div",{"class":"title"}) 

for item in g_data: 
     Desp = item.contents[1].text 
     Location = item.contents[5].text 
     Address = item.contents[3].text 
     #Print Desp,Location,Address 
     #Problem is over here,The result set that i am getting is quite scatter. I want to put it into 
     #pandas object and then output to excel. 
     output_df = pd.concat([Desp,Location,Address]) 

私は何をしようとしていますか? 私は取得しているデータセットをExcelにエクスポートします。エクセルに書き込むため

+0

Despは何ですか?場所と住所は同じです。 – MYGz

答えて

1

がちょうどdf.to_excel('file_name', index=None)

チェックこの操作を行います。

import requests 
import pandas as pd 
from bs4 import BeautifulSoup 

url="http://property.sulekha.com/apartments-flats-in-pimpri-chinchwad-general-pune-for-sale" 
r = requests.get(url) 
soup = BeautifulSoup(r.content) 
df = pd.DataFrame(columns = ['Desp', 'Location', 'Address']) 
g_data = soup.find_all("div",{"class":"title"}) 
for item in g_data: 
    Desp = ' '.join(item.contents[1].text.split()) 
    loc = ' '.join(item.contents[5].text.split()).split(',') 
    Location = loc[0][9:] 
    Address = loc[1] 
    df = df.append({'Desp':Desp, 'Location':Location, 'Address': Address}, ignore_index=True) 

df.head() 

を出力:

enter image description here

関連する問題