错误 'Unexpected HTTP code on the target page'、'status_code':当我尝试使用代理 api 请求 json url 时出现 403
Error 'Unexpected HTTP code on the target page', 'status_code': 403 when I try to request a json url with a proxy api
我正在尝试废弃此网站 https://triller.co/ , so I want to get information from profile pages like this https://triller.co/@warnermusicarg , what I do is trying to request the json url that contains the information, in this case it's https://social.triller.co/v1.5/api/users/by_username/warnermusicarg
当我使用 requests.get() 它正常工作,我可以检索所有信息。
import requests
import urllib.parse
from urllib.parse import urlencode
url = 'https://social.triller.co/v1.5/api/users/by_username/warnermusicarg'
headers = {'authority':'social.triller.co',
'method':'GET',
'path':'/v1.5/api/users/by_username/warnermusicarg',
'scheme':'https',
'accept':'*/*',
'accept-encoding':'gzip, deflate, br',
'accept-language':'ar,en-US;q=0.9,en;q=0.8',
'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsImlhdCI6MTY0MDc4MDc5NSwiZXhwIjoxNjkyNjIwNzk1fQ.eyJpZCI6IjUyNjQ3ODY5OCJ9.Ds-acbfcGSeUrGDSs47pBiT3b13Eb9SMcB8BF8OylqQ',
'origin':'https://triller.co',
'sec-ch-ua':'" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'sec-ch-ua-mobile':'?0',
'sec-ch-ua-platform':'"Windows"',
'sec-fetch-dest':'empty',
'sec-fetch-mode':'cors',
'sec-fetch-site':'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
response = requests.get(url, headers=headers)
当我尝试使用 API 代理提供商作为 Webscraping.ai、ScrapingBee 等时出现问题
api_key='my_api_key'
api_url='https://api.webscraping.ai/html?'
params = {'api_key': api_key, 'timeout': '20000', 'url':url}
proxy_url = api_url + urlencode(params)
response2 = requests.get(proxy_url, headers=headers)
这给了我这个错误
2022-01-08 22:30:59 [urllib3.connectionpool] DEBUG: https://api.webscraping.ai:443 "GET /html?api_key=my_api_key&timeout=20000&url=https%3A%2F%2Fsocial.triller.co%2Fv1.5%2Fapi%2Fusers%2Fby_username%2Fwarnermusicarg&render_js=false HTTP/1.1" 502 91
{'status_code': 403, 'status_message': '', 'message': 'Unexpected HTTP code on the target page'}
我尝试做的是:
1- 我在我的 API 代理提供商的 documentation 中搜索了 403 代码的含义,它说 api_key 是错误的,但我 100% 确定它是正确的,
另外,我换了另一个 API 代理提供商,但同样的问题,
另外,我对 twitter.com 也有同样的问题
我不知道该怎么办?
目前,问题的代码成功 returns 代码为 200 的响应,但有 2 个可能的问题:
- 有些网站会阻止数据中心代理,请尝试使用
proxy=residential
API 参数 (params = {'api_key': api_key, 'timeout': '20000', proxy: 'residential', 'url':url}
)。
headers
参数中的某些 header 是不必要的。 Webscraping.AI 使用自己的一组 header 来模仿普通浏览器的行为,因此设置自定义 user-agent、accept-language 等可能会干扰它们并导致 403 响应从目标网站。仅使用必要的 headers。看起来在你的情况下它只是 authorization
header。
我不知道到底是什么导致了这个错误,但我尝试使用他们的 webscraping_ai.ApiClient() 实例,就像在 here 中一样,它起作用了,
configuration = webscraping_ai.Configuration(
host = "https://api.webscraping.ai",
api_key = {
'api_key': 'my_api_key'
}
)
with webscraping_ai.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = webscraping_ai.HTMLApi(api_client)
url_j = url # str | URL of the target page
headers = headers
timeout = 20000
js = False
proxy = 'datacenter'
api_response = api_instance.get_html(url_j, headers=headers, timeout=timeout, js=js, proxy=proxy)
我正在尝试废弃此网站 https://triller.co/ , so I want to get information from profile pages like this https://triller.co/@warnermusicarg , what I do is trying to request the json url that contains the information, in this case it's https://social.triller.co/v1.5/api/users/by_username/warnermusicarg 当我使用 requests.get() 它正常工作,我可以检索所有信息。
import requests
import urllib.parse
from urllib.parse import urlencode
url = 'https://social.triller.co/v1.5/api/users/by_username/warnermusicarg'
headers = {'authority':'social.triller.co',
'method':'GET',
'path':'/v1.5/api/users/by_username/warnermusicarg',
'scheme':'https',
'accept':'*/*',
'accept-encoding':'gzip, deflate, br',
'accept-language':'ar,en-US;q=0.9,en;q=0.8',
'authorization': 'Bearer eyJhbGciOiJIUzI1NiIsImlhdCI6MTY0MDc4MDc5NSwiZXhwIjoxNjkyNjIwNzk1fQ.eyJpZCI6IjUyNjQ3ODY5OCJ9.Ds-acbfcGSeUrGDSs47pBiT3b13Eb9SMcB8BF8OylqQ',
'origin':'https://triller.co',
'sec-ch-ua':'" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'sec-ch-ua-mobile':'?0',
'sec-ch-ua-platform':'"Windows"',
'sec-fetch-dest':'empty',
'sec-fetch-mode':'cors',
'sec-fetch-site':'same-site',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'}
response = requests.get(url, headers=headers)
当我尝试使用 API 代理提供商作为 Webscraping.ai、ScrapingBee 等时出现问题
api_key='my_api_key'
api_url='https://api.webscraping.ai/html?'
params = {'api_key': api_key, 'timeout': '20000', 'url':url}
proxy_url = api_url + urlencode(params)
response2 = requests.get(proxy_url, headers=headers)
这给了我这个错误
2022-01-08 22:30:59 [urllib3.connectionpool] DEBUG: https://api.webscraping.ai:443 "GET /html?api_key=my_api_key&timeout=20000&url=https%3A%2F%2Fsocial.triller.co%2Fv1.5%2Fapi%2Fusers%2Fby_username%2Fwarnermusicarg&render_js=false HTTP/1.1" 502 91
{'status_code': 403, 'status_message': '', 'message': 'Unexpected HTTP code on the target page'}
我尝试做的是: 1- 我在我的 API 代理提供商的 documentation 中搜索了 403 代码的含义,它说 api_key 是错误的,但我 100% 确定它是正确的, 另外,我换了另一个 API 代理提供商,但同样的问题, 另外,我对 twitter.com 也有同样的问题 我不知道该怎么办?
目前,问题的代码成功 returns 代码为 200 的响应,但有 2 个可能的问题:
- 有些网站会阻止数据中心代理,请尝试使用
proxy=residential
API 参数 (params = {'api_key': api_key, 'timeout': '20000', proxy: 'residential', 'url':url}
)。 headers
参数中的某些 header 是不必要的。 Webscraping.AI 使用自己的一组 header 来模仿普通浏览器的行为,因此设置自定义 user-agent、accept-language 等可能会干扰它们并导致 403 响应从目标网站。仅使用必要的 headers。看起来在你的情况下它只是authorization
header。
我不知道到底是什么导致了这个错误,但我尝试使用他们的 webscraping_ai.ApiClient() 实例,就像在 here 中一样,它起作用了,
configuration = webscraping_ai.Configuration(
host = "https://api.webscraping.ai",
api_key = {
'api_key': 'my_api_key'
}
)
with webscraping_ai.ApiClient(configuration) as api_client:
# Create an instance of the API class
api_instance = webscraping_ai.HTMLApi(api_client)
url_j = url # str | URL of the target page
headers = headers
timeout = 20000
js = False
proxy = 'datacenter'
api_response = api_instance.get_html(url_j, headers=headers, timeout=timeout, js=js, proxy=proxy)