簡體   English   中英

Error 'Unexpected HTTP code on the target page', 'status_code': 403 when I try to request a json url with a proxy api

[英]Error 'Unexpected HTTP code on the target page', 'status_code': 403 when I try to request a json url with a proxy api

I'm trying to scrap this website 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包含信息,在這種情況下它是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代理提供者的文檔中搜索了403代碼的含義,它說api_key是錯誤的,但我100%確定它是正確的,另外,我換成了另一個API代理提供商,但同樣的問題,另外,我有同樣的問題 twitter.com 我不知道該怎么辦?

目前,問題上的代碼成功返回代碼為 200 的響應,但有 2 個可能的問題:

  1. 一些站點阻止數據中心代理,嘗試使用proxy=residential API 參數( params = {'api_key': api_key, 'timeout': '20000', proxy: 'residential', 'url':url} )。
  2. headers參數中的某些標題是不必要的。 Webscraping.AI 使用自己的一組標頭來模仿普通瀏覽器的行為,因此設置自定義用戶代理、接受語言等可能會干擾它們並導致目標站點的 403 響應。 僅使用必要的標題。 看起來它只是你的情況下的authorization header 。

我不知道究竟是什么導致了這個錯誤,但我嘗試使用他們的 webscraping_ai.ApiClient() 實例,就像這里一樣,它起作用了,

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM