如果页面上不存在元素,则跳过项目

这是我的整个代码。


response = requests.get("https://www.zomato.com/san-francisco/restaurants?q=restaurants&page=" + str(i),headers=headers)

content = response.content

bs = BeautifulSoup(content, "html.parser")


zomato_containers = bs.find_all("div", {"class": "search-snippet-card"})


for zomato_container in zomato_containers:

    title = zomato_container.find("a", {"class": "result-title"}).get_text()

    numVotes = zomato_container.select_one('[class^=rating-votes-div]').text  

    numVotes = numVotes[1] if len(numVotes) > 1 else numVotes[0]


    print("restaurant_title: ", title)

    print("numVotes: ", numVotes)

我收到一个错误:


“numVotes = zomato_container.select_one('[class^=rating-votes-div]').text AttributeError: 'NoneType' 对象没有属性 'text'”


我非常肯定这是因为页面上的某些元素不存在。我试图跳过这些元素,但无法弄清楚如何。


非常感谢。我非常感激。


偶然的你
浏览 179回答 2
2回答

慕尼黑5688855

最简单的方法是:for zomato_container in zomato_containers:    title = zomato_container.find("a", {"class": "result-title"}).get_text()    try:        numVotes = zomato_container.select_one('[class^=rating-votes-div]').text          numVotes = numVotes[1] if len(numVotes) > 1 else numVotes[0]    except AttributeError:        continue    print("restaurant_title: ", title)    print("numVotes: ", numVotes)

紫衣仙女

您可以在尝试访问text属性之前测试变量是否为 Noneimport requestsfrom bs4 import BeautifulSoup as bsi = 1headers = {'User-Agent': 'Mozilla/5.0'}response = requests.get("https://www.zomato.com/san-francisco/restaurants?q=restaurants&page=" + str(i),headers=headers)content = response.contentsoup = bs(content, "html.parser")zomato_containers = soup.select('.search-snippet-card')for zomato_container in zomato_containers:    title = zomato_container.find("a", {"class": "result-title"}).get_text()    numVotes = zomato_container.select_one('[class^=rating-votes-div]')    if numVotes is None:        numVotes = 'N/A'    else:        numVotes = numVotes.text.strip()    print("restaurant_title: ", title)    print("numVotes: ", numVotes)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python