我是新手。我想从tripadvisor.com中提取用户位置。但是,当我对位置命令使用if-else时,这会跳过没有位置的数据的输出。例如: 先前的结果: url ='https://www.tripadvisor.com.au/Hotel_Review-g55857-d98853-Reviews-Knights_Inn_Fort_Worth-Fort_Worth_Texas.html'
name title review
Knights Inn Fort Worth A dump aa..
Knights Inn Fort Worth 30 nimute stay bb..
Knights Inn Fort Worth Business cc..
...
添加位置后:
name title review location
Knights Inn Fort Worth 30 nimute stay bb.. Gulfport
Knights Inn Fort Worth Business cc.. Tyler, Texas
...
因为第一个标题没有用户的位置,所以它跳过了,只提供了带有位置的结果。如何在不忽略其他部分数据的情况下提取用户的位置?
使用硒。代码:
for hname in browser.find_elements_by_id("HEADING"):
name = hname.text
for reviewBox in browser.find_elements_by_class_name('reviewSelector'):
htitle = reviewBox.find_element_by_class_name("noQuotes")
title = htitle.text
hreview = reviewBox.find_element_by_class_name("quote+ .prw_reviews_text_summary_hsx .partial_entry")
review = hreview.text
for loc in reviewBox.find_elements_by_class_name("userLoc"):
if loc == None:
location = [" "]
else:
location = loc.text
请帮助指出我错了,并提供更好的解决方案将是很好的。感谢您的时间。 预期输出:
name title review location
Knights Inn Fort Worth A dump aa..
Knights Inn Fort Worth 30 nimute stay bb.. Gulfport
Knights Inn Fort Worth Business cc.. Tyler, Texas