このエラーが発生します:
NameError: name 'htmltext' is not defined
それは以下のコードから来ています:
from bs4 import BeautifulSoup
import urllib
import urllib.parse
url = "http://nytimes.com"
urls = [url]
visited = [url]
while len(urls) > 0:
try:
htmltext = urllib.urlopen(urls[0]).read()
except:
print(urls[0])
soup = BeautifulSoup(htmltext)
urls.pop(0)
print(soup.findAll('a',href = true))
Python 3.xでは、のurllib.request
代わりにインポートする必要がありurllib
ます。次に、行を変更します。
htmltext = urllib.urlopen(urls[0]).read()
に:
htmltext = urllib.request.urlopen(urls[0]).read()
最後に、に変更true
しTrue
ます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加