2016-10-13 6 views
0

現在、各国のトリップアドバイザーレストランからデータを取得しようとしています。私が引き出そうとしている分野は、名前、住所、料理の種類(中国語、ステーキハウスなど)です。スクリプトを使用して名前と住所を取得できました。しかし、自分のために食べ物の種類を引き出すことはかなり困難です。あなたが下を見れば、あなたが私のものからキャプチャしようとしているもののスクリーンショットと私のコードを見つけるでしょう。料理ジャンルからプーリ料理

What I want to pull from TripAdvisor is circled in red.

When I print my code it keeps printing 'Asian' even thought the second one should be a 'Steakhouse'.

#import libraries 
import requests 
from bs4 import BeautifulSoup 
import csv 

#loop to move into the next pages. entries are in increments of 30 per page 
for i in range(0, 120, 30): 
    #need this here for when you want more than 30 entries pulled 
    while i <= range: 
     i = str(i) 
     #url format offsets the restaurants in increments of 30 after the oa 
     url1 = 'https://www.tripadvisor.com/Restaurants-g294217-oa' + i + '-Hong_Kong.html#EATERY_LIST_CONTENTS' 
     r1 = requests.get(url1) 
     data1 = r1.text 
     soup1 = BeautifulSoup(data1, "html.parser") 
     for link in soup1.findAll('a', {'property_title'}): 
      #print 'https://www.tripadvisor.com/Restaurant_Review-g294217-' + link.get('href') 
      restaurant_url = 'https://www.tripadvisor.com/Restaurant_Review-g294217-' + link.get('href') 
      #print link.string 
      account_name = link.string.strip() 
      #cuisine type pull 
      for link in soup1.findAll('a', {'cuisine'}): 
       cuisinetype = link.string.strip() 
      r_address = requests.get(restaurant_url) 
      r_addresstext = r_address.text 
      soup2 = BeautifulSoup(r_addresstext, "html.parser") 
      for restaurant_url in soup2.findAll('span', {'street-address'})[0]: 
       #print(restaurant_url.string) 
       rest_address = restaurant_url.string 
       rest_array = [account_name, rest_address, cuisinetype] 
       print rest_array 
       #with open('ListingsPull-HongKong.csv', 'a') as file: 
        #writer = csv.writer(file) 
        #writer.writerow([account_name, rest_address]) 
     break 

答えて

0

このアプローチは、特にエレガントではありませんが、あなたに許容できるかもしれません。私はあなたが望む情報が「料理」の「詳細」タブの下で繰り返されているようだと気付きました。私はこの方法で簡単にアクセスできることを発見しました。

>>> import requests 
>>> from bs4 import BeautifulSoup 
>>> restaurant_url='https://www.tripadvisor.ca/Restaurant_Review-g294217-d2399904-Reviews-Tin_Lung_Heen-Hong_Kong.html' 
>>> soup2 = BeautifulSoup(requests.get(restaurant_url).text, "html.parser") 
>>> street_address=soup2.find('span',{'street-address'}) 
>>> street_address 
<span class="street-address" property="streetAddress">International Commerce Centre, 1 Austin Road West, Kowloon</span> 
>>> street_address.contents[0] 
'International Commerce Centre, 1 Austin Road West, Kowloon' 
>>> for item in soup2.findAll('div', attrs={'class', 'title'}): 
...  if 'Cuisine' in item.text: 
... 
...   item.text.strip() 
...   break 
...   
'Cuisine' 
>>> content=item.findNext('div', attrs={'class', 'content'}) 
>>> content 
<div class="content"> 
Chinese, Asian 
</div> 
>>> content.text 
'\nChinese,\xa0Asian\n' 
>>> content.text.strip().split('\xa0') 
['Chinese,', 'Asian'] 
+0

あなたのソリューションは大部分の作業をしていますが、バックスラッシュを追加するとpycharmが文字列を認識しないため、 '\ xa0'を分割するのが苦労しています。これについての考えは? – dtrinh

+0

ああ。私はあなたの前に 'r'が必要だと思います。 r '\ xa0' –

+0

文字列を認識するためのプログラムがあります。しかし、私はまだスプリット機能を正しく実行することができません。それでも、HTMLの全行を印刷しています。 – dtrinh

関連する問題