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
あなたのソリューションは大部分の作業をしていますが、バックスラッシュを追加するとpycharmが文字列を認識しないため、 '\ xa0'を分割するのが苦労しています。これについての考えは? – dtrinh
ああ。私はあなたの前に 'r'が必要だと思います。 r '\ xa0' –
文字列を認識するためのプログラムがあります。しかし、私はまだスプリット機能を正しく実行することができません。それでも、HTMLの全行を印刷しています。 – dtrinh