AttributeError : '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)
사용하고 있습니다.
이 Python 2.x에서 작동합니다.
파이썬 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"에서 사용할 수 있습니다.
얻을려면 ' DATAX을 = urllib.urlopen (URL) .read () '썬에서 작업 3 (이 파이에 대한 올바른했을 때 2 ) 당신은 그냥이 작은 일들을 변경해야합니다.
1 : urllib 문 자체 (중간에 .request 추가) :
dataX = urllib.request.urlopen(url).read()
2 : 앞에 오는 import 문 ( 'import urlib'에서 다음으로 변경)
import urllib.request
그리고 그것은 python3에서 작동합니다. :)
import urllib.request as ur
filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
print(line.strip())
전형적인 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)
python2.x에서 사용되는 코드는 다음과 같이 사용할 수 있습니다.
from urllib.request import urlopen
urlopen(url)
그렇고, 요청은 다른 모델이 사용하기 더 많은 것을 제안하면 pip install 그것을 사용할 수 있으며 다음과 같이 사용할 수 있습니다.
import requests
requests.get(url)
requests.post(url)
사용하기 많은 것을 생각하고 저도입니다 .... 하하
가능한 방법 중 하나 :
import urllib
...
try:
# Python 2
from urllib2 import urlopen
except ImportError:
# Python 3
from urllib.request import urlopen
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)
참고 URL : https://stackoverflow.com/questions/3969726/attributeerror-module-object-has-no-attribute-urlopen
'IT' 카테고리의 다른 글
내용을 변경하는 방법 (0) | 2020.07.13 |
---|---|
UIImage를 NSData로 변환하고 Swift에서 UIImage로 다시 변환 하시겠습니까? (0) | 2020.07.13 |
Java의 명령 행 진행률 표시 줄 (0) | 2020.07.13 |
Go 언어로 동일한 이름의 다른 패키지를 가져오고 사용하는 방법은 무엇입니까? (0) | 2020.07.13 |
ssrs 보고서에서 날짜 및 시간을 어떻게 형식화합니까? (0) | 2020.07.13 |