第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如果頁(yè)面上不存在元素,則跳過(guò)項(xiàng)目

如果頁(yè)面上不存在元素,則跳過(guò)項(xiàng)目

偶然的你 2022-04-27 16:27:04
這是我的整個(gè)代碼。response = requests.get("https://www.zomato.com/san-francisco/restaurants?q=restaurants&page=" + str(i),headers=headers)content = response.contentbs = 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)我收到一個(gè)錯(cuò)誤:“numVotes = zomato_container.select_one('[class^=rating-votes-div]').text AttributeError: 'NoneType' 對(duì)象沒(méi)有屬性 'text'”我非??隙ㄟ@是因?yàn)轫?yè)面上的某些元素不存在。我試圖跳過(guò)這些元素,但無(wú)法弄清楚如何。非常感謝。我非常感激。
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

最簡(jiǎn)單的方法是:


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)



查看完整回答
反對(duì) 回復(fù) 2022-04-27
?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗(yàn) 獲得超15個(gè)贊

您可以在嘗試訪問(wèn)text屬性之前測(cè)試變量是否為 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)


查看完整回答
反對(duì) 回復(fù) 2022-04-27
  • 2 回答
  • 0 關(guān)注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)