특성 오류: 'module' 개체에 'urlopen' 특성이 없습니다.
웹 사이트의 HTML 소스 코드를 파이썬으로 다운로드하려고 하는데 이 오류가 발생합니다.
Traceback (most recent call last):
File "C:\Users\Sergio.Tapia\Documents\NetBeansProjects\DICParser\src\WebDownload.py", line 3, in <module>
file = urllib.urlopen("http://www.python.org")
AttributeError: 'module' object has no attribute 'urlopen'
저는 여기 가이드를 따르고 있습니다: http://www.boddie.org.uk/python/HTML.html
import urllib
file = urllib.urlopen("http://www.python.org")
s = file.read()
f.close()
#I'm guessing this would output the html source code?
print(s)
저는 파이썬 3을 사용하고 있습니다.
이 기능은 Python 2.x에서 작동합니다.
Python 3의 경우 문서를 확인합니다.
import urllib.request
with urllib.request.urlopen("http://www.python.org") as url:
s = url.read()
# I'm guessing this would output the html source code ?
print(s)
Python 2+3 호환 솔루션은 다음과 같습니다.
import sys
if sys.version_info[0] == 3:
from urllib.request import urlopen
else:
# Not Python 3 - today, it is most likely to be Python 2
# But note that this might need an update when Python 4
# might be around one day
from urllib import urlopen
# Your code where you can use urlopen
with urlopen("http://www.python.org") as url:
s = url.read()
print(s)
import urllib.request as ur
s = ur.urlopen("http://www.google.com")
sl = s.read()
print(sl)
Python v3에서 "urllib.request"는 그 자체로 모듈이기 때문에 "urllib"는 여기서 사용할 수 없습니다.
python3에서 작동하는 'dataX = urllib.urlopen(url.read)'을(를) 얻으려면(이것은 python2의 경우 옳았을 것입니다) 두 가지 작은 사항만 변경해야 합니다.
1: urlib 문 자체(가운데에 .request 추가):
dataX = urllib.request.urlopen(url).read()
2: 앞에 있는 가져오기 문('importurlib'에서 다음으로 변경):
import urllib.request
그리고 그것은 python3에서 작동해야 합니다 :)
두 줄 변경:
import urllib.request #line1
#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2
오류 403: 금지 오류 예외가 발생한 경우 다음을 시도하십시오.
siteurl = "http://www.python.org"
req = urllib.request.Request(siteurl, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.100 Safari/537.36'})
pageHTML = urllib.request.urlopen(req).read()
당신의 문제가 해결되기를 바랍니다.
import urllib.request as ur
filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
print(line.strip())
python 3의 경우 다음과 같은 방법을 사용합니다.
import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
현재 작업 디렉토리에 비디오를 다운로드합니다.
python3용 솔루션:
from urllib.request import urlopen
url = 'http://www.python.org'
file = urlopen(url)
html = file.read()
print(html)
import urllib
import urllib.request
from bs4 import BeautifulSoup
with urllib.request.urlopen("http://www.newegg.com/") as url:
s = url.read()
print(s)
soup = BeautifulSoup(s, "html.parser")
all_tag_a = soup.find_all("a", limit=10)
for links in all_tag_a:
#print(links.get('href'))
print(links)
가능한 방법 중 하나:
import urllib
...
try:
# Python 2
from urllib2 import urlopen
except ImportError:
# Python 3
from urllib.request import urlopen
코드가 Python 버전 2.x를 사용하는 경우 다음 작업을 수행할 수 있습니다.
from urllib.request import urlopen
urlopen(url)
그건 그렇고, 저는 다음과 같은 다른 모듈을 제안합니다.requests
사용하기 더 편리한 방법입니다.사용할 수 있습니다.pip
설치하고 다음과 같이 사용합니다.
import requests
requests.get(url)
requests.post(url)
타사 사용six
모듈을 사용하여 Python2와 Python3 간에 코드를 호환할 수 있도록 합니다.
from six.moves import urllib
urllib.request.urlopen("<your-url>")
imgResp = urllib3.request.RequestMethods.urlopen(url)
추가할 내용RequestMethods
urlopen을 사용하기 전에
언급URL : https://stackoverflow.com/questions/3969726/attributeerror-module-object-has-no-attribute-urlopen
'it-source' 카테고리의 다른 글
목록에 다른 목록이 있는지 확인합니다. (0) | 2023.05.06 |
---|---|
문자열에서 클래스 인스턴스 만들기 (0) | 2023.05.06 |
집계하는 동안 MongoDB에 $second에 다른 if가 있습니까? (0) | 2023.05.06 |
postgresql에서 배열 크기를 찾는 방법 (0) | 2023.05.06 |
나머지 화면 공간의 높이를 div로 채웁니다. (0) | 2023.05.06 |