我正在解析网站yelp.com,我正在获取菜名name_of_dishs=yelp_beat.findAll('div',{'class':'lemon--div__373c0__1mboc businessName__373c0__1fTgn border-color--default__373c0__2oFDT'})
(Soco,SalaThai,Bunker),而且我还需要获得菜肴的评论,但是当我使用嵌套循环时,它不起作用
import requests
from bs4 import BeautifulSoup
base_url = "https://www.yelp.com/search?find_desc=Restaurants&find_loc=New%20York%2CNY&start=30"
yelp = requests.get(base_url)
yelp_beat = BeautifulSoup(yelp.text, 'html.parser')
name_of_dishs=yelp_beat.findAll('div',{'class':'lemon--div__373c0__1mboc businessName__373c0__1fTgn border-color--default__373c0__2oFDT'})
for dish in name_of_dishs:
#print(dish.text)
for reviews in dish.findAll('span',{'lemon--span__373c0__3997G text__373c0__2pB8f reviewCount__373c0__2r4xT text-color--mid__373c0__3G312 text-align--left__373c0__2pnx_'}):
print(reviews.text)
答案 0 :(得分:0)
它缺少class
作为参数,我简化了选择器并选择了li
yelp_beat = BeautifulSoup(yelp.text, 'html.parser')
theList = yelp_beat.select('.mainContentContainer__373c0__32Mqa .domtags--li__373c0__3TKyB.list-item__373c0__M7vhU')
for li in theList:
name_of_dishs = li.select_one('h3 a')
reviews = li.select_one('.reviewCount__373c0__2r4xT')
if not name_of_dishs or not reviews:
continue
print('{}: {}'.format(name_of_dishs.text, reviews.text))
结果
Jajaja: 576 reviews
Balzem: 364 reviews
Jane: 2995 reviews
PMF Pardon My French: 830 review