Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HTTP error 404
#1
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?
Reply
#2
The url "https://api.aoikujira.com/zip/xml.get.php" does not exist.
Reply
#3
PS: Just a suggestion, try using **http://api.aoikujira.com/ip/xml** this instead
Reply
#4
!/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>
Reply
#5
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']
'トウキョウト'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTTP 404 error with Session Pool Clives 0 301 Jun-17-2021, 06:45 PM
Last Post: Clives
  error HTTP Error 403: Forbidden local_bit 1 1,245 Nov-14-2020, 11:34 AM
Last Post: ndc85430
  urllib.error.HTTPError: HTTP Error 404: Not Found ckkkkk 4 4,661 Mar-03-2020, 11:30 AM
Last Post: snippsat
  python beginner HTTP Error 500 leofcastro 0 832 Jan-24-2020, 04:37 PM
Last Post: leofcastro
  How to check HTTP error 500 and bypass SriMekala 3 2,443 May-04-2019, 02:07 PM
Last Post: snippsat
  Syntax error for HTTP request GET THX1138 1 4,597 May-12-2018, 12:02 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020