Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

python - For-loop gives output and when run again shows none

I have written the below code:

source2 = url.urlopen("https://www.yelp.com.au/biz/mcfit-berlin-26?osq=gym")
soup = BeautifulSoup(source2, "html.parser")
full_address = soup.find_all("address", class_ = "lemon--address__373c0__2sPac")
for f in full_address:
    print(f.text)

When I run it (for the very first time) it gives me the desired output. But if I run it again, it shows nothing (even no error). In the same code in another part (below),

source2 = url.urlopen("https://www.yelp.com.au/biz/mcfit-berlin-26?osq=gyms")
soup = BeautifulSoup(source2, "html.parser")
link_info = soup.find_all("p", class_ = "lemon--p__373c0__3Qnnj text__373c0__2Kxyz text-color--normal__373c0__3xep9 text-align--left__373c0__2XGa- text-weight--semibold__373c0__2l0fe text-size--large__373c0__3t60B")


for webadd in link_info[0]:
    print(webadd.text)

it will give me a result but if run again will give me the error "IndexError: list index out of range". Can anybody tell me why this strange behaviour of Jupyter Notebook.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...