Python Beautifulsoup索引在循环中超出范围
我正试图与beautifulsoup一起浏览一个城市的餐馆列表及其详细信息(排名、地址……) 在第一步中,我获取所有餐厅的路径列表,并将它们存储在变量Python Beautifulsoup索引在循环中超出范围,python,python-3.x,web-scraping,beautifulsoup,indexoutofrangeexception,Python,Python 3.x,Web Scraping,Beautifulsoup,Indexoutofrangeexception,我正试图与beautifulsoup一起浏览一个城市的餐馆列表及其详细信息(排名、地址……) 在第一步中,我获取所有餐厅的路径列表,并将它们存储在变量mydivs中,然后我尝试循环列表并获取每个餐厅的详细信息,这是我的代码: for link in mydivs: print(link["href"]) url2=URL = 'https://www.yelp.fr'+link["href"] page2 = requests.get(URL) soup2 =
mydivs
中,然后我尝试循环列表并获取每个餐厅的详细信息,这是我的代码:
for link in mydivs:
print(link["href"])
url2=URL = 'https://www.yelp.fr'+link["href"]
page2 = requests.get(URL)
soup2 = BeautifulSoup(page2.text)
if (len(soup2.find_all("span",{"class":"lemon--span__373c0__3997G display--inline__373c0__2q4au border-color--default__373c0__YEvMS"}))!=0):
address=soup2.find_all("p",{"class":"lemon--p__373c0__3Qnnj text__373c0__2pB8f text-color--normal__373c0__K_MKN text-align--left__373c0__2pnx_ text-weight--bold__373c0__3HYJa"})
if (len(address[0].findChildren("span" , recursive=False))==0):
print("dep"+address[0].findChildren("span" , recursive=False)[0].text)
else:
print("dep"+address[0].findChildren("a" , recursive=False)[0].text)
print("adre"+address[1].findChildren("span" , recursive=False)[0].text)
print("Beki adress"+soup2.find_all("p",{"class":"lemon--p__373c0__3Qnnj text__373c0__2pB8f text-color--normal__373c0__K_MKN text-align--left__373c0__2pnx_"})[0].text)
print("tel"+soup2.find_all("div",{"class":"lemon--div__373c0__1mboc island__373c0__3fs6U u-padding-t1 u-padding-r1 u-padding-b1 u-padding-l1 border--top__373c0__19Owr border--right__373c0__22AHO border--bottom__373c0__uPbXS border--left__373c0__1SjJs border-color--default__373c0__2oFDT background-color--white__373c0__GVEnp"})[0].findChildren("div" , recursive=False)[0].findChildren("div" , recursive=False)[0].findChildren("div" , recursive=False)[1].findChildren("p" , recursive=False)[1].text)
print("etoile"+soup2.find_all("span",{"class":"lemon--span__373c0__3997G display--inline__373c0__2q4au border-color--default__373c0__YEvMS"})[0].findChildren("div",recursive=False)[0]["aria-label"])
我得到了一个错误:
索引器:列表索引超出范围
在线:
print("dep"+address[0].findChildren("a" , recursive=False)[0].text)
我正在努力修复此错误,并始终在第一次打印时得到它。同意上述评论。如果页面内没有锚定标记,则它将返回“索引超出范围异常” 您可以通过下面的代码来验证这一点 x=汤。芬达尔(“a”)
如果上面返回None,则页面中没有a标记。我猜您
findChildren
返回None
不是列表,因为adress对象中没有“a”。@Fourier,它将返回空列表。如果它返回None
,错误将是NoneType不可下标
。
if x is not None and len(x) > 0:
section = x[0]