问题:User-Agent报invalid syntax语法错误
问题相关代码:见下方
运行结果及报错内容:报错在User-Agent里有语法错误,可是我明明是直接复制粘贴过来的。
import requests
from bs4 import BeautifulSoup
headers={
"Cookies":"BIDUPSID=55FF8AD9EB53F70CFA1CC861DFFBEAC2; PSTM=1562143532; BAIDUID=28042DB4DEEC020DBC1A769DAE921A46:FG=1; __yjs_duid=1_cb8ec8647ce56b196c3450b9e4f6a9181620180217393; MCITY=-289:; BD_UPN=12314753; BDUSS=I2cUVUVEpzeDZBTXB-VXZFaFotVUhEV0tnS2Y5TzRNcFJEdWdEVWdHcXJQcTFpSVFBQUFBJCQAAAAAAAAAAAEAAAC15sY5eWlmYW5fa2FpYm8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKuxhWKrsYViYX; BDUSS_BFESS=I2cUVUVEpzeDZBTXB-VXZFaFotVUhEV0tnS2Y5TzRNcFJEdWdEVWdHcXJQcTFpSVFBQUFBJCQAAAAAAAAAAAEAAAC15sY5eWlmYW5fa2FpYm8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKuxhWKrsYViYX; delPer=0; BD_CK_SAM=1; PSINO=5; ZFY=KOmJpaRWNbJnOa53TdcEOjSmW:BpBQ2MFJnduj:AfSNgc:C; BA_HECTOR=208g8584al24a00ka51havn4i14; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BD_HOME=1; BAIDUID_BFESS=722685BBC25A520BFE862BE0FCE368F0:FG=1; channel=baidusearch; Hm_lvt_aec699bb6442ba076c8981c6dc490771=1654404567,1655716727; shifen[390204880322_67336]=1655716737; BCLID=12688218398199553649; BDSFRCVID=ffkOJeC629CT7WjD4a2bUslQ1m5rbsOTH6aop9vLpR4RMSUyacd-EG0P2U8g0KuMyjkWogKK0eOTHkAF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF=tbKj_KtatC03jtb9q4jhKP_0bgTJ2b8s5DcW2hcH0KLKbqO2jpJ8Q50_Dx5Pb-7ItCDtQJobBMb1MRjvK5r-Xb8wjb5uQhQ8KC7bsl5TtUtKSDnTDMRhXJFqMnJyKMnitIT9-pno0lQrh459XP68bTkA5bjZKxtq3mkjbPbDfn02eCKu-n5jHjj-eHuO3H; BCLID_BFESS=12688218398199553649; BDSFRCVID_BFESS=ffkOJeC629CT7WjD4a2bUslQ1m5rbsOTH6aop9vLpR4RMSUyacd-EG0P2U8g0KuMyjkWogKK0eOTHkAF_2uxOjjg8UtVJeC6EG0Ptf8g0f5; H_BDCLCKID_SF_BFESS=tbKj_KtatC03jtb9q4jhKP_0bgTJ2b8s5DcW2hcH0KLKbqO2jpJ8Q50_Dx5Pb-7ItCDtQJobBMb1MRjvK5r-Xb8wjb5uQhQ8KC7bsl5TtUtKSDnTDMRhXJFqMnJyKMnitIT9-pno0lQrh459XP68bTkA5bjZKxtq3mkjbPbDfn02eCKu-n5jHjj-eHuO3H; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; ZD_ENTRY=baidu; baikeVisitId=d1b7df71-5e27-48c0-ad88-0e98ac1b97d4; COOKIE_SESSION=2710_1_9_8_1_9_1_0_9_5_0_3_0_0_0_0_1655461275_1655716738_1655719579|9#11502408_1091_1655716727|9; Hm_lpvt_aec699bb6442ba076c8981c6dc490771=1655719986; sugstore=1; H_PS_PSSID=36429_36545_36625_36592_36454_31253_36452_36422_36165_36520_26350_36469_22159; BDSVRTM=0; ab_sr=1.0.1_ZDliYTBlNGFkOTU4OWFjNDNlNTcyNjUyNGNlODU0YjI1MzQ1ZjlmZTFkMDhmMjVjMjBhM2JhMzM5NjUyMjllZTNhZjg0OTExZDJlYmU3YTJlZjM5MDk3YjIzYzNlN2JlZGU1OThmYmMyZmUwMDU2MTg1NGFhZDM1MGFhY2U0YTU3ZTM3NTcyZTBhNWFhZWYyNzVkNWI5MDA5MmMyOTlhMA=="
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"
}
url='https://www.baidu.com/s?wd=python&rsv_spt=1&rsv_iqid=0x824d1421003d3813&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&oq=beautifulsoup%25E8%25BF%2590%25E8%25A1%258C%25E5%2587%25BA%25E7%258E%25B0none&rsv_btype=t&inputT=3269&rsv_t=da77%2FWLTn2u87bDDJetWtOEmzbHLP7v1XmR18jXM6beUOyI0IjMyRTPZB2Og54lSaNoo&rsv_pq=e73e4f520000fe31&rsv_sug3=70&rsv_sug1=67&rsv_sug7=100&rsv_sug2=0&rsv_sug4=4819'
html=requests.get(url, headers=headers)
html.encoding=html.apparent_encoding
soup=BeautifulSoup(html.text,'lxml')
title=soup.select("div.result.c-container h3")
for i in title:
print(i.text)