Python 标红怎么办

accept-language 中的-标红

img

代码来自b站一位up主,运行的时候出现了这个问题,请问怎么修改呢?

headers变量里面的单引号感觉是中文状态下的?全部改为英文状态的单引号,get_content改成下面这样就可以了。

img

import requests
from bs4 import BeautifulSoup
def get_content(url):
    #url="https://s.weibo.com/weibo?q=%e5%8f%8c%e5%87%8f&nodup=1"
    headers={
        'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
        'accept-encoding':'gzip, deflate, br',
        'accept-language':'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5',
        'Connection':'keep-alive',
        'Cookie':'SINAGLOBAL=2002793067242.412.1621560743299; UOR=login.sina.com.cn,service.weibo.com,login.sina.com.cn; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9W5U9Kpif6NXhCY6fs52ujLr5JpX5KMhUgL.FozXeoBN1K.p1Kz2dJLoIpxPeEH8SCHFBE-ReFH8SC-Rxb-4eBtt; ALF=1677161678; SSOLoginState=1645625679; SCF=AmX3HjMJH4xXPUgn6XTsnfaokCAXDWkwlVCBEzdyfnbQTJYtlrzWnDiZTrLEy1OX-DF3417m6pgo_s_TLQVIq7k.; SUB=_2A25PEjEfDeRhGeRK6VYW-SfNwj6IHXVsZiXXrDV8PUNbmtB-LWLAkW9NU5SDBpxtGsXoNRKEfX8IyfSQl20TRs9e; _s_tentry=login.sina.com.cn; Apache=1952758910288.248.1645625713164; ULV=1645625713307:17:5:2:1952758910288.248.1645625713164:1645407993563',
        'Host':'s.weibo.com',
        'Referer':'https://s.weibo.com/weibo?q=%e5%8f%8c%e5%87%8f&nodup=1',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'
    }
    response=requests.get(url,headers=headers)
    html=BeautifulSoup(response.content,'lxml')
    conetnt=html.find_all('div',class_='card-wrap')
    for ct in conetnt:
        user_info=ct.find_all('a',class_='name')
        if user_info!=[]:
            user_name=user_info[0].text
            user_index='https:'+user_info[0]['href']
            #...其他代码题主自己打下
            print(user_name,user_index)


get_content('https://s.weibo.com/weibo?q=%e5%8f%8c%e5%87%8f&nodup=1')

img

字符串单引号好多写成了中文全角的,要改成英文半角的。
可以用编辑器的替换功能把中文全角的单引号(‘和’),替换成英文半角的 (')

img

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img