다음 beautifulsoup 스크립트는 출력을 표시하지 않습니다. 내가 놓친 것이 있습니까? 지문 중 일부를 치기위한 것이 었습니다.
from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys
url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"
def getTitle(url):
try:
html = urlopen(url)
except HTTPError as e:
print(e)
return None
try:
bsObj = BeautifulSoup(html.read())
except AttributeError as e:
return None
return bsObj
title = getTitle(url1)
if title == None:
print("None at URL: " + url1)
else:
print(title)
당신 문제는 마침내 ... 신원입니다.
from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys
url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"
def getTitle(url):
try:
html = urlopen(url)
except HTTPError as e:
print(e)
return None
try:
bsObj = BeautifulSoup(html.read())
except AttributeError as e:
return None
return bsObj
title = getTitle(url1)
if title == None:
print("None at URL: " + url1)
else:
print(title)
귀하의 문제는 return bsObj
함수가 print
s 를 실행하지 못하게하는 것 입니다. 함수가 인쇄 할 수있는 유일한 것은 HTTPError
또는 ArgumentError
.
를 반환 bsObj
하려면 함수를 return
종료 하기 때문에 함수의 끝에서 반환해야합니다 .
아, 그리고 무조건적으로 함수를 재귀하므로 어쨌든 StackOverflow가됩니다.
from urllib.request import urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup
import sys
url1 = "https://www.youtube.com/watch?v=APmUWC8S1_M"
def getTitle(url):
try:
html = urlopen(url)
except HTTPError as e:
print(e)
return None
try:
bsObj = BeautifulSoup(html.read())
except AttributeError as e:
return None
title = getTitle(url1) # Infinite recursion
if title == None:
print("None at URL: " + url1)
else:
print(title)
return bsObj # Moved to the end
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다