Ik probeer Python te gebruiken om de HTML-broncode van een website te downloaden, maar ik ontvang deze fout.
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'
Ik volg de gids hier: 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)
Ik gebruik Python 3.
Antwoord 1, Autoriteit 100%
Dit werkt in Python 2.x.
Voor Python 3 Kijk in de Docs :
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)
Antwoord 2, Autoriteit 8%
Een Python 2 + 3-compatibele oplossing is:
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)
Antwoord 3, autoriteit 6%
import urllib.request as ur
s = ur.urlopen("http://www.google.com")
sl = s.read()
print(sl)
In Python v3 is de “urllib.request” een module op zich, daarom kan “urllib” hier niet worden gebruikt.
Antwoord 4, autoriteit 3%
Om ‘dataX = urllib.urlopen(url).read()‘ te laten werken in python3(dit zou zijn correct geweest voor python2)je moet gewoon 2 kleine dingen veranderen.
1:de urllib-instructie zelf (voeg de .request in het midden toe):
dataX = urllib.request.urlopen(url).read()
2:Het importstatement dat eraan voorafgaat (wijzig van ‘import urlib’ in:
import urllib.request
En het zou moeten werken in python3 🙂
Antwoord 5
import urllib.request as ur
filehandler = ur.urlopen ('http://www.google.com')
for line in filehandler:
print(line.strip())
Antwoord 6
Wijzig TWEE regels:
import urllib.request #line1
#Replace
urllib.urlopen("http://www.python.org")
#To
urllib.request.urlopen("http://www.python.org") #line2
Als u ERROR 403: Forbidden Error-uitzondering krijgt, probeer dan dit:
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()
Ik hoop dat uw probleem is opgelost.
Antwoord 7
Probeer zoiets als dit voor Python 3:
import urllib.request
urllib.request.urlretrieve('http://crcv.ucf.edu/THUMOS14/UCF101/UCF101/v_YoYo_g19_c02.avi', "video_name.avi")
Het zal de video downloaden naar de huidige werkdirectory
Antwoord 8
Oplossing voor Python3:
from urllib.request import urlopen
url = 'http://www.python.org'
file = urlopen(url)
html = file.read()
print(html)
Antwoord 9
een van de mogelijke manier om het te doen:
import urllib
...
try:
# Python 2
from urllib2 import urlopen
except ImportError:
# Python 3
from urllib.request import urlopen
Antwoord 10
Gebruik zes module om u CODE compatibel tussen Python2 en Python3
urllib.request.urlopen("<your-url>")```
Antwoord 11
Uw code gebruikt in Python2.x, u kunt als volgt gebruiken:
from urllib.request import urlopen
urlopen(url)
Trouwens, stel voor dat een andere module genaamd requests
gebruiksvriendelijker is, je kunt pip
gebruiken om deze te installeren en als volgt te gebruiken:
import requests
requests.get(url)
requests.post(url)
Ik dacht dat het gemakkelijk te gebruiken was, ik ben ook een beginner….hahah
Antwoord 12
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)
Antwoord 13
imgResp = urllib3.request.RequestMethods.urlopen(url)
Voeg deze RequestMethods
toe voordat u urlopen gebruikt