如何发送 Python 中数据的获取请求?
How to send a get request with data in Python?
我想从这个站点获取数据:
https://www.techstars.com/portfolio?category=all%20companies
如您在网络选项卡中所见,已向此 link:
发送一个获取请求
https://datacore.techstars.com/companies?order=name&program_status=in.(session_in_progress,session_over)}&type=eq.Graduate&session=not.in.(%22%22)&offset=0&limit=50
但是当我打开它时它说“权限被拒绝...”并且当我在 Python.
中发送 get 请求时也是如此
如何使用正确的数据向此 link 发送获取请求?
这是我的代码。
import requests
url = 'https://datacore.techstars.com/companies?order=name&program_status=in.(session_in_progress,session_over)}&type=eq.Graduate&session=not.in.(%22%22)&offset=0&limit=50'
payload = {'order':'name','program_status':'in.(session_in_progress,session_over)}', 'type':'eq.Graduate','session':'not.in.(%22%22)','offset':'0','limit':'50'}
r = requests.get(url, data=payload)
r.content
它给了我这个结果
b'{"hint":null,"details":null,"code":"42501","message":"permission denied for table companies"}'
您还需要提供额外的 header 才能让您的请求生效。例如:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicGdyZXN0X3d3dzIifQ.RB9HicmPNEl4C0Ree9SVw3Oh5tinjDiIbBurBujVnEg',
'Accept' : 'application/json, text/plain, */*',
'Origin' : 'https://www.techstars.com'
}
url = "https://datacore.techstars.com/companies?order=name&program_status=in.(session_in_progress,session_over)}&type=eq.Graduate&session=not.in.(%22%22)&id=in.(001E000001EZFcYIAX,001E000000I0FdNIAV,001E000000SsjXdIAJ,001E000000HzxB9IAJ,001E000000IyUe7IAF,001E000000HzKYCIA3,001E000000IIItfIAH,001E000000IyUJRIA3)}&offset=0&limit=50"
r = requests.get(url, headers=headers)
for entry in r.json():
print(f"{entry['name']} - {entry['description']}")
需要 Authorizaton
header。此值可能在主页的 HTML 内。
这将为您提供如下输出:
Chainalysis - Building the compliance layer for the future of value transfer.
ClassPass - ClassPass is a membership program for fitness classes across multiple gyms and studios, making working out more accessible.
DataRobot - DataRobot brings AI technology and ROI enablement services to global enterprises.
DigitalOcean - The cloud for developers
Outreach - Outreach is a sales engagement platform that accelerates revenue growth by optimizing interactions throughout the customer lifecycle.
Remitly - Remitly is a mobile payments service that enables users to make person-to-person international money transfers.
SendGrid - SendGrid is a cloud-based customer communication platform that drives engagement and business growth.
Zipline - Zipline is creating a highly automated drone network to shuttle blood and pharmaceuticals to remote locations in hours rather than weeks or months.
我想从这个站点获取数据:
https://www.techstars.com/portfolio?category=all%20companies
如您在网络选项卡中所见,已向此 link:
发送一个获取请求
https://datacore.techstars.com/companies?order=name&program_status=in.(session_in_progress,session_over)}&type=eq.Graduate&session=not.in.(%22%22)&offset=0&limit=50
但是当我打开它时它说“权限被拒绝...”并且当我在 Python.
中发送 get 请求时也是如此如何使用正确的数据向此 link 发送获取请求?
这是我的代码。
import requests
url = 'https://datacore.techstars.com/companies?order=name&program_status=in.(session_in_progress,session_over)}&type=eq.Graduate&session=not.in.(%22%22)&offset=0&limit=50'
payload = {'order':'name','program_status':'in.(session_in_progress,session_over)}', 'type':'eq.Graduate','session':'not.in.(%22%22)','offset':'0','limit':'50'}
r = requests.get(url, data=payload)
r.content
它给了我这个结果
b'{"hint":null,"details":null,"code":"42501","message":"permission denied for table companies"}'
您还需要提供额外的 header 才能让您的请求生效。例如:
import requests
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoicGdyZXN0X3d3dzIifQ.RB9HicmPNEl4C0Ree9SVw3Oh5tinjDiIbBurBujVnEg',
'Accept' : 'application/json, text/plain, */*',
'Origin' : 'https://www.techstars.com'
}
url = "https://datacore.techstars.com/companies?order=name&program_status=in.(session_in_progress,session_over)}&type=eq.Graduate&session=not.in.(%22%22)&id=in.(001E000001EZFcYIAX,001E000000I0FdNIAV,001E000000SsjXdIAJ,001E000000HzxB9IAJ,001E000000IyUe7IAF,001E000000HzKYCIA3,001E000000IIItfIAH,001E000000IyUJRIA3)}&offset=0&limit=50"
r = requests.get(url, headers=headers)
for entry in r.json():
print(f"{entry['name']} - {entry['description']}")
需要 Authorizaton
header。此值可能在主页的 HTML 内。
这将为您提供如下输出:
Chainalysis - Building the compliance layer for the future of value transfer.
ClassPass - ClassPass is a membership program for fitness classes across multiple gyms and studios, making working out more accessible.
DataRobot - DataRobot brings AI technology and ROI enablement services to global enterprises.
DigitalOcean - The cloud for developers
Outreach - Outreach is a sales engagement platform that accelerates revenue growth by optimizing interactions throughout the customer lifecycle.
Remitly - Remitly is a mobile payments service that enables users to make person-to-person international money transfers.
SendGrid - SendGrid is a cloud-based customer communication platform that drives engagement and business growth.
Zipline - Zipline is creating a highly automated drone network to shuttle blood and pharmaceuticals to remote locations in hours rather than weeks or months.