如果页面上没有元素,则跳过项目

时间:2019-08-08 07:49:23

标签: python python-3.x beautifulsoup

Here是我的整个代码。

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'”

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

非常感谢。非常感谢。

2 个答案:

答案 0 :(得分:1)

最简单的方法是:

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)

答案 1 :(得分:0)

您可以在尝试访问text属性之前测试变量是否为None

import requests
from bs4 import BeautifulSoup as bs

i = 1
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get("https://www.zomato.com/san-francisco/restaurants?q=restaurants&page=" + str(i),headers=headers)
content = response.content
soup = 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)