简体   繁体   中英

Skip item if element doesn't exist on page

Here is my entire code.

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)

I get an error :

"numVotes = zomato_container.select_one('[class^=rating-votes-div]').text AttributeError: 'NoneType' object has no attribute 'text'"

I am highly positive it's because some elements on the page do not exist. I am trying to skip those elements but cannot figure out how.

Thank you so much. I greatly appreciate it.

The simplest way would be:

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)

You can test if variable is None before attempting to access text property

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM