Python Forum
HTTP error 404 - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: HTTP error 404 (/thread-18699.html)



HTTP error 404 - Karin - May-28-2019

hi there

I was trying to get address by using API, but I got HTTP Error. Does it mean URL I code didn't make sense or only I couldn't get data from the URL?

My code is here.
import urllib.request
import urllib.parse

API = "https://api.aoikujira.com/zip/xml.get.php"

#パラメータをURLエンコードする
values = {
    'fmt' : 'xml',     
    'zn' : '1740064'    
}
params = urllib.parse.urlencode(values)

#リクエスト用のURLを作成
url = API + "?" + params
print("url = ", url)

#ダウンロード
data = urllib.request.urlopen(url).read()
text = data.decode("utf-8")
print(text)


And the Error is this
Error:
url = https://api.aoikujira.com/zip/xml.get.php?fmt=xml&zn=1740064 Traceback (most recent call last): File "download-zip.py", line 18, in <module> data = urllib.request.urlopen(url).read() File "C:\Users\…\Python\Python36-32\lib\urllib\request.py", line 223, in urlopen return opener.open(url, data, timeout) File "C:\Users\…\Python\Python36-32\lib\urllib\request.py", line 532, in open response = meth(req, response) File "C:\Users\…\Python\Python36-32\lib\urllib\request.py", line 642, in http_response 'http', request, response, code, msg, hdrs) File "C:\Users\…\Python\Python36-32\lib\urllib\request.py", line 570, in error return self._call_chain(*args) File "C:\Users\…\Python\Python36-32\lib\urllib\request.py", line 504, in _call_chain result = func(*args) File "C:\Users\…\Python\Python36-32\lib\urllib\request.py", line 650, in http_error_default raise HTTPError(req.full_url, code, msg, hdrs, fp) urllib.error.HTTPError: HTTP Error 404: Not Found
How can I solve it?


RE: HTTP error 404 - heiner55 - May-28-2019

The url "https://api.aoikujira.com/zip/xml.get.php" does not exist.


RE: HTTP error 404 - pixel_chick - May-31-2019

PS: Just a suggestion, try using **http://api.aoikujira.com/ip/xml** this instead


RE: HTTP error 404 - heiner55 - May-31-2019

!/usr/bin/python3
import urllib.request
import urllib.parse

API = "http://api.aoikujira.com/ip/xml"

#パラメータをURLエンコードする
values = {
    'fmt' : 'xml',.....
    'zn' : '1740064'....
}
params = urllib.parse.urlencode(values)
.
#リクエスト用のURLを作成
url = API + "?" + params
print("url = ", url)
.
#ダウンロード
data = urllib.request.urlopen(url).read()
text = data.decode("utf-8")
print(text)
Output:
<?xml version="1.0" encoding="utf-8" ?> <ip> <API_URI>http://api.aoikujira.com/ip/get.php</API_URI> <REMOTE_ADDR>77.190.78.29</REMOTE_ADDR> <REMOTE_HOST>x4dbe4e1d.dyn.telefonica.de</REMOTE_HOST> <REMOTE_PORT>54742</REMOTE_PORT> <HTTP_HOST>api.aoikujira.com</HTTP_HOST> <HTTP_USER_AGENT>Python-urllib/3.7</HTTP_USER_AGENT> <HTTP_ACCEPT_LANGUAGE></HTTP_ACCEPT_LANGUAGE> <HTTP_ACCEPT_CHARSET></HTTP_ACCEPT_CHARSET> <SERVER_PORT>80</SERVER_PORT> <FORMAT>xml</FORMAT> </ip>



RE: HTTP error 404 - snippsat - May-31-2019

It also better to always use Requests
If get xml back also need to use parser eg BeautifulSoup.
import requests
from bs4 import BeautifulSoup

fmt = 'xml'
zn = '1740064'
url = f'https://api.aoikujira.com/zip/zip.php?fmt={fmt}&zn={zn}'
response = requests.get(url).content
soup = BeautifulSoup(response, 'lxml')
Example usage:
>>> [i for i in soup.find_all('result')]
[<result name="api.aoikujira.com/zip"></result>,
 <result version="1.00"></result>,
 <result request_zip_num="1740064"></result>,
 <result result_code="1"></result>,
 <result database="2018-07-30"></result>]

>>> att = soup.find('value').attrs
>>> att
{'ken_kana': 'トウキョウト'}
>>> att['ken_kana']
'トウキョウト'
Using fmt=json will Requests(encode json()) and give back a Python dictionary.
import requests

fmt = 'json'
zn = '1740064'
url = f'https://api.aoikujira.com/zip/zip.php?fmt={fmt}&zn={zn}'
response = requests.get(url).json()
Example usage:
>>> response
{'API_URL': 'http://api.aoikujira.com/zip/zip.php',
 'address': '中台',
 'address_kana': 'ナカダイ',
 'city': '板橋区',
 'city_kana': 'イタバシク',
 'database': '2018-07-30',
 'ken_kana': 'トウキョウト',
 'result': '東京都板橋区中台',
 'result_code': 1,
 'state': '東京都',
 'version': '1.01'}

# Call is simple as it's a dictionary 
>>> response['ken_kana']
'トウキョウト'