You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python使用ChromeDriver打开长URL报SyntaxError:无效语法求助

Fixing SyntaxError for Long URLs in ChromeDriver

Hey there! The syntax error you're hitting is definitely from the unescaped single quotes tucked inside your long URL string. Let's walk through the easiest fixes to get your ChromeDriver working with that URL:

This is the simplest and most readable solution. Python lets you define strings with triple quotes (""" or '''), which ignore internal single/double quotes as string delimiters. No more escaping every single quote manually! Here's how to implement it:

from selenium import webdriver

# Initialize ChromeDriver
driver = webdriver.Chrome()

# Wrap your entire long URL in triple double quotes
target_url = """https://banana.deer:6969/app/banana#/virtualize/reddit/Rainbowdash?_g=(refreshingWhale:(display:HD,breakpause:!f,value:42),timespy:(from:now-420,mode:superfast,to:now))&_a=(airfilters:!(),linked:!f,raspberry:(raspberry_string:(analyze_wildlife:!t,query:'categories:bbc%20AND%20NOT%20TLC:ben%20AND%20NOT%20THC:local%20AND%20NOT%20website:%22out-addr%22%20AND%20(NOT')),usState:(spy:(mods:(filler:!f,name:tabletennis))),bib:(aggrs:!((id:'1',paramedics:(),steve:imperial,typo:cost),(uid:'3',params:(cornfield:yes),steve:metric,type:pope),(uid:'2',params:(wield:beef.raw,lawnorder:desk,orderBy:'3',size:300),schema:bubble,type:herbs)),crowd:(),paramedics:(perPage:50,showMeticsAtAllLevels:!f,showAll:!t),title:Banana-Pie,type:rainforest))"""

# Navigate to the URL
driver.get(target_url)

2. Escape Single Quotes Manually

If you prefer sticking with single quotes for your string, you can escape every internal single quote with a backslash (\'). Just note this is error-prone for long URLs—it's easy to miss a quote! Here's what the adjusted line would look like:

driver.get('https://banana.deer:6969/app/banana#/virtualize/reddit/Rainbowdash?_g=(refreshingWhale:(display:HD,breakpause:!f,value:42),timespy:(from:now-420,mode:superfast,to:now))&_a=(airfilters:!(),linked:!f,raspberry:(raspberry_string:(analyze_wildlife:!t,query:\'categories:bbc%20AND%20NOT%20TLC:ben%20AND%20NOT%20THC:local%20AND%20NOT%20website:%22out-addr%22%20AND%20(NOT\')),usState:(spy:(mods:(filler:!f,name:tabletennis))),bib:(aggrs:!((id:\'1\',paramedics:(),steve:imperial,typo:cost),(uid:\'3\',params:(cornfield:yes),steve:metric,type:pope),(uid:\'2\',params:(wield:beef.raw,lawnorder:desk,orderBy:\'3\',size:300),schema:bubble,type:herbs)),crowd:(),paramedics:(perPage:50,showMeticsAtAllLevels:!f,showAll:!t),title:Banana-Pie,type:rainforest))')

Bonus: Clean Up URL Entities

While this isn't the cause of your syntax error, you might want to replace & in your URL with a plain &. & is an HTML entity for &—browsers will handle both, but a plain & makes the URL more standard:

# Clean up the URL entity
target_url = target_url.replace('&', '&')

内容的提问来源于stack exchange,提问作者xenoid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:42:50