0
我對Webscraping相當陌生,在網上看了一些教程視頻之後給了我一個嘗試。我決定使用Tripadvisor.com
並嘗試從顧客評論收集數據。用Python刮臉BeautifulSoup 4
這是我想出了(代碼):
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
my_url = 'https://www.tripadvisor.com.sg/Attraction_Review-g293916-d12033454-
Reviews-SHOW_DC-Bangkok.html'
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div",{"class":"ui_column is-9"})
for container in containers:
rating = container.div.div.div.span["class"]
comment_container = container.p
comment = comment_container[0]
print("rating" + rating)
print("comment" + comment)
這是我的代碼的輸出:
Traceback (most recent call last):
File "trip_advisor.py", line 18, in <module>
comment = comment_container[0]
File "/anaconda/lib/python3.6/site-packages/bs4/element.py", line 1011, in
__getitem__
return self.attrs[key]
KeyError: 0
誰能幫我解決這個問題?謝謝。