Im using get request to get data from a site.
captured reqeuest details :
Query String Parameters
s: vrnf8
following is my code . But i get 404 not found status code.
import requests
url = "https://www.fleetmon.com/search/?s=vrnf8"
pload = {"s":"vrnf8"}
resp = requests.get(url, params=pload)
print(resp.text)
print(resp.status_code)
please help me with this !
request capture file : https://filebin.net/ilx668e5yrdmbu7p
response must be this :
[[{"lastreport_class": "position-age-live", "course": 213, "callsign": "VRNF8", "lastreport_verbose": "2020-07-17 12:28 UTC", "speed": 15.6, "destination": "SGSIN PEBGA", "vessel_url": "/vessels/seaspan-amazon_9630391_8896228/", "imo": "9630391", "location": "South Kuroshio, JP", "latitude": 28.732660, "vesselid": "8896228", "mmsi": "477390400", "lastreport_timestamp": 1594985298, "lastreport_short": "21 min", "name_clean": "SEASPAN AMAZON", "vessel_type": "Container ship", "master_image_id": 2278121, "flag_id": "HK", "icon": "cargo", "is_moving": true, "name": "SEASPAN AMAZON", "longitude": 28.732660, "length": "337", "flag_name": "Hong Kong SAR of China"}], 1, [], 0]
Try this code below:
import requests
url = "https://www.fleetmon.com/search/?s=vrnf8"
headers = {
'X-Requested-With': 'XMLHttpRequest'
}
response = requests.get(url, headers=headers)
print(response.json())
Result:
[[{'lastreport_class': 'position-age-live', 'course': 212, 'callsign': 'VRNF8', 'lastreport_verbose': '2020-07-17 13:22 UTC', 'speed': 15.9, 'destination': 'SGSIN PEBGA', 'vessel_url': '/vessels/seaspan-amazon_9630391_8896228/', 'imo': '9630391', 'location': 'South Kuroshio, JP', 'latitude': 28.540143, 'vesselid': '8896228', 'mmsi': '477390400', 'lastreport_timestamp': 1594988550, 'lastreport_short': '1 min', 'name_clean': 'SEASPAN AMAZON', 'vessel_type': 'Container ship', 'master_image_id': 2278121, 'flag_id': 'HK', 'icon': 'cargo', 'is_moving': True, 'name': 'SEASPAN AMAZON', 'longitude': 28.540143, 'length': '337', 'flag_name': 'Hong Kong SAR of China'}], 1, [], 0]
import requests
url = "https://www.fleetmon.com/search/?s=vrnf8"
pload = {"s":"vrnf8"}
resp = requests.get(url, params=pload)
print(resp.text)
print(resp.status_code)
NOTE: The url you have typed doesn't exist
Correct Code
import requests
url = "https://www.fleetmon.com/search/"
pload = {"s":"vrnf8"}
resp = requests.get(url, params=pload)
print(resp.text)
print(resp.status_code)
But Now also you will get an error
Explanation
let's understand what's happening in your code
import requests
url = "https://www.fleetmon.com/search/?s=vrnf8"
pload = {"s":"vrnf8"}
resp = requests.get(url, params=pload)
'''
resp is equivalent to request.get("https://www.fleetmon.com/search/?s=vrnf8/?s=vrnf8")
when we make get request to url, it appends /?query=value to it's requested url
and this url doesn't exist so you will get 404
'''
print(resp.text)
print(resp.status_code)
code that I sent
```python
import requests
url = "https://www.fleetmon.com/search"
pload = {"s":"vrnf8"}
resp = requests.get(url, params=pload)
'''
resp is equivalent to request.get("https://www.fleetmon.com/search/?s=vrnf8")
when we make get request to url, it again appends /?query=value to it's requested url
and even this url also doesn't exist so you will again get 404
'''
print(resp.text)
print(resp.status_code)
NOTE: You can check weather a url exist or not by typing it in google search bar
Related
My audio for podcast and art for it on google drive, in doc says to make like this, but it returns 400 error
import requests
podcast_file = "https://drive.google.com/file/d/1w0IDXVotyHKEGSbBWUxEoDvaJQRsWCJO"
headers = {
'Authorization': 'Token here my token',
'Content-Type': 'application/json',
}
new_episode = {
"title":"Too many or too few?",
"description":"",
"summary":"",
"artist":"Muffin Man",
"tags":"",
"duration":23462,
"guid":"Buzzsprout788880",
"episode_number":1,
"season_number":1,
"explicit":False,
"private":False,
"email_user_after_audio_processed": False,
"audio_url": podcast_file,
"artwork_url": "https://drive.google.com/file/d/1xoNKAbTYX8GjVg0ZB_ekFUl8ljQICMZv"
}
url = 'https://www.buzzsprout.com/api/episodes.json'
req = requests.post(url, data=new_episode, headers=headers)
print(req.text)
print(req.status_code)
I am requesting to mindbodyapi to get token with the following code using requests library
def get_staff_token(request):
URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
payload = {
'Api-Key': API_KEY,
'SiteId': "1111111",
'Username': 'user#xyz.com',
'Password': 'xxxxxxxx',
}
r = requests.post(url=URL, params=payload)
print(r.text)
return HttpResponse('Done')
gives a response as follows
{"Error":{"Message":"Missing API key","Code":"DeniedAccess"}}
But if I request the following way it works, anybody could tell me, what I am doing wrong on the above code.
conn = http.client.HTTPSConnection("api.mindbodyonline.com")
payload = "{\r\n\t\"Username\": \"username\",\r\n\t\"Password\": \"xxxxx\"\r\n}"
headers = {
'Content-Type': "application/json",
'Api-Key': API_KEY,
'SiteId': site_id,
}
conn.request("POST", "/public/v6/usertoken/issue", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
In the second one, you are passing the API Key in headers and the credentials in the body of the request. In the first, you are sending both the API Key and credentials together in the query string, not the request body. Refer to requests.request() docs
Just use two dictionaries like in your second code and the correct keywords, I think it should work:
def get_staff_token(request):
URL = "https://api.mindbodyonline.com/public/v6/usertoken/issue"
payload = {
'Username': 'user#xyz.com',
'Password': 'xxxxxxxx',
}
headers = {
'Content-Type': "application/json",
'Api-Key': API_KEY,
'SiteId': "1111111",
}
r = requests.post(url=URL, data=payload, headers=headers)
print(r.text)
return HttpResponse('Done')
I would get access to this website, I get it from another website, https://www.betexplorer.com/soccer/algeria/ligue-1/bordj-bou-arreridj-tlemcen/tfvAHu7U/, in the Network section of Developer tools.
Code
import requests
url = 'https://www.betexplorer.com/archive-odds/4urejxv464x0xd4645/18/'
response = requests.get(url, headers={'User-Agent': 'my user agent'})
print(response)
Output <Response [404]>
Set Referer HTTP header to obtain correct response:
import json
import requests
url = "https://www.betexplorer.com/archive-odds/4urejxv464x0xd4645/18/"
headers = {
"Referer": "https://www.betexplorer.com",
}
data = requests.get(url, headers=headers).json()
print(json.dumps(data, indent=4))
Prints:
[
{
"date": "23.07. 17:36",
"odd": "1.88",
"change": "+0.08"
},
{
"date": "23.07. 17:34",
"odd": "1.80",
"change": "-0.01"
},
...
I want to send a multipart/form-data as a post body using python request but I am not getting bad request issue.
import requests
headers = {"Content-Type": "multipart/form-data"}
data = {
"#context": "http://semantro.com/", "#type": "KiranaSearch", "actionName": "listCategoryProducts",
"pageLimit": {"#context": "http://semantro.com/", "#type": "PageProperty", "start": 0, "end": 24},
"data": {"#context": "http://semantro.com/", "#type": "KiranaCategory",
"identifier": "c5394d1d5c6c4cb8-adc77dd996876dba"}
}
response = requests.post('https://merokirana.com/semantro-web-interface/query',
data=data, headers=headers)
print(response.text)
Response
{
"statusTitle" : "ServiceUnsuccessful",
"statusMessage" : "Invalid type of data received. The request should have multipart query data.",
"#context" : "http://semantro.com",
"#type" : "RemoteServiceStatus"
}
But I can retrieve the required data using postman the same formdata.
import http.client
import mimetypes
from codecs import encode
conn = http.client.HTTPSConnection("merokirana.com")
dataList = []
boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
dataList.append(encode('--' + boundary))
dataList.append(encode('Content-Disposition: form-data; name=data;'))
dataList.append(encode('Content-Type: {}'.format('text/plain')))
dataList.append(encode(''))
dataList.append(encode("{ \"#context\": \"http://semantro.com/\", \"#type\": \"KiranaSearch\", \"actionName\": \"listCategoryProducts\",\"pageLimit\": {\"#context\": \"http://semantro.com/\", \"#type\": \"PageProperty\", \"start\": 0, \"end\": 24}, \"data\": {\"#context\": \"http://semantro.com/\", \"#type\": \"KiranaCategory\",\"identifier\": \"c5394d1d5c6c4cb8-adc77dd996876dba\"}}"))
dataList.append(encode('--'+boundary+'--'))
dataList.append(encode(''))
body = b'\r\n'.join(dataList)
payload = body
headers = {
'Content-type': 'multipart/form-data; boundary={}'.format(boundary)
}
conn.request("POST", "/semantro-web-interface/query", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
output:
I want to update the title of a pull request and performing the below to achieve it :- (followed this doc https://developer.github.com/v3/pulls/#update-a-pull-request)
data = {"title": "New title"}
url='https://hostname/api/v3/repos/owner/repo/pulls/80'
token = 'my-token'
headers = {'Content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'token %s' % token}
resp = requests.patch(url, data=json.dumps(data), headers=headers)
print resp.json()
What am I missing ? Please help.
The following worked for me:
import requests
token = "my-token"
url = "https://api.github.com/repos/:owner/:repo/pulls/:number"
payload = {
"title": "New title"
}
r = requests.patch(url, auth=("username", token), json=payload)
print r.json()