使用chromedp访问速卖通英文页面遭重定向至阿拉伯页面的解决方法
解决方案:通过添加请求头强制访问英文页面
可以通过添加请求头的方式解决这个问题,速卖通的页面语言判定会参考请求头中的语言偏好信息,配置合适的Accept-Language头即可强制优先加载英文页面。
修改后的代码
在你的chromedp执行步骤中,添加chromedp.SetExtraHTTPHeaders来设置请求头,将其放在Navigate步骤之前,确保请求一开始就携带指定头信息:
err := chromedp.Run(ctx, chromedp.EmulateViewport(1920, 1980), // 设置强制英文的请求头 chromedp.SetExtraHTTPHeaders(map[string]interface{}{ "Accept-Language": "en-US,en;q=0.9", // 优先英文(美国地区) }), // Navigate to AliExpress with English language and US region chromedp.Navigate(`https://www.aliexpress.com/?language=en®ion=US`), chromedp.WaitVisible(`body`, chromedp.ByQuery), chromedp.Sleep(5*time.Second), // Wait for the page to fully load // Extract all links with their text and href chromedp.Evaluate(` JSON.stringify( Array.from(document.querySelectorAll('a')).map(a => ({ text: a.textContent.trim(), href: a.href })) ) `, &links), ) if err != nil { log.Fatal(err) }
关键说明
Accept-Language: en-US,en;q=0.9告诉服务器,当前客户端优先接受美国英文,其次是通用英文;- 配合URL参数
language=en®ion=US,双重确保服务器返回英文页面; - 如果仍然存在重定向问题,可以尝试额外添加Cookie头(比如预先设置速卖通的地域Cookie),但
Accept-Language是最直接有效的解决方案。
内容的提问来源于stack exchange,提问作者Flissi Hamed
相关产品推荐
相关产品推荐

