2017-01-31 3 views
0

私はPythonの新機能ですので、これはおそらく簡単でインデントの問題かもしれません。私は美しいスープを使っていくつかのウェブページを掻き集め、後でデータを操作するために使用できる辞書のリストを作成しようとしています。辞書のループで削り取ったときに同じWebサイトが呼び出される

コードは正常に動作しているようですが、(liste_flat)で終わるリストは同じ2つの辞書のリストに過ぎません。私は別の辞書のリストが欲しい。

def scrap_post(url): 
    url = "https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/".format(i) 
    dictionary = {} 
    response = requests.get(url) 
    soup = bs(response.text,"lxml") 
    taille = len(soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")) #48 entries 
    for num_ville in range(0,taille): 
     print(num_ville) 
     apt_id = soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")[num_ville]['data-id'] 
     entry = soup.find_all("div", class_="col-sm-6 col-md-4 col-lg-3 pl-grid-prop not-viewed ")[num_ville] 
     pricepw = soup.find_all('div', class_='col-xs-5 col-sm-4 price')[num_ville].find('h3').text.encode('utf-8').replace('\xc2\xa3','',).replace('pw','',).strip() 
     rooms = soup.find_all('div', class_='col-xs-6 type')[num_ville].find('p').text.encode('utf-8').strip() 
     lat = soup.find_all('div', {"itemprop":"geo"})[num_ville].find('meta', {'itemprop':'latitude'})['content'] 
     lon = soup.find_all('div', {"itemprop":"geo"})[num_ville].find('meta', {'itemprop':'longitude'})['content'] 
     dictionary[num_ville]={'Price per week':pricepw,'Rooms':rooms,'Latitude':lat,'Longitude':lon} 
    return dictionary 

#get all URLs 
liste_url = [] 
liste_url = ['https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/''%i' %i for i in range(1,3)] 

#get flats 
liste_flat = [scrap_post(i) for i in liste_url] 

何とか同じWebサイトで2回ループする必要があります。どのように私は別のウェブサイト上でループしていることを確認する上で任意のアドバイスですか?

ありがとうございます!

答えて

0

はい、機能に変数urlがハードコードされているため、同じWebサイトでループしています。

url = "https://www.findproperly.co.uk/property-to-rent-london/commute/W3siaWQiOjkxMDYsImZyZXEiOjUsIm1ldGgiOiJwdWJ0cmFucyIsImxuZyI6LTAuMTI0Nzg5LCJsYXQiOjUxLjUwODR9XQ==/max-time/90/page/".format(i) 

意味は、関数に送信するものに関係なく、常にこのURLを使用します。それを削除したいかもしれません。また、文字列にプレースホルダを配置しておらず、.format(i)は本質的に何もしません。

+0

ありがとう、それはまさに問題でした!私はちょうどあなたが推奨するように最初のURLを取り出した、それは完全に動作しています。 – aliki43

関連する問題