Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用脚本搜索bing结果会导致编码问题_Python_Python 3.x_Character Encoding_Bing - Fatal编程技术网

Python 使用脚本搜索bing结果会导致编码问题

Python 使用脚本搜索bing结果会导致编码问题,python,python-3.x,character-encoding,bing,Python,Python 3.x,Character Encoding,Bing,为了获得我的单词列表中每个单词的搜索结果数,我编写了以下内容: with open ("C:\wordslist.txt") as f: lines = f.readlines() def bingSearch(word): r = requests.get('http://www.bing.com/search', params={'q':'"'+word+'"'} ) soup = Beauti

为了获得我的
单词列表中每个单词的搜索结果数,我编写了以下内容:

with open ("C:\wordslist.txt") as f:
    lines = f.readlines()

def bingSearch(word):   
    r = requests.get('http://www.bing.com/search',
                 params={'q':'"'+word+'"'}
                )
    soup = BeautifulSoup(r.text, "html.parser")
    return (soup.find('span',{'class':'sb_count'}))

matches = [re.search(regex,line).groups() for line in lines]
for match in matches:       
    searchWord = match[0]
    found = bingSearch(searchWord)
    print (found.text)
它工作得很好,我得到了准确的结果,但包含特殊字符的单词除外,例如:
“número”

如果我调用
bingSearch(“número”)
我会得到一个准确的结果。 如果我调用
bingSearch(匹配[0])
(其中打印
match[0]
会产生
“número”
),我会得到不准确的结果

我尝试过类似于
str(match[0])
match[0].encode(encoding=“UTF-8”)
,但没有成功


有什么想法吗?

打开文件时,请尝试直接给出编码,这样会有所不同

打开(“C:\wordslist.txt”,encoding=“utf-8”)作为f:


打开文件时,请尝试直接给出编码,这样会有所不同

打开(“C:\wordslist.txt”,encoding=“utf-8”)作为f:


您是否尝试过检查导线上的情况?也就是说,您是否尝试过使用一些网络嗅探工具或跟踪工具,或者任何可以显示传出HTTP GET请求的外观的工具?似乎您需要调试bingSearch函数的内部,而不仅仅是随机尝试外部功能。此外,您还需要指定“不成功”以外的内容。你没有得到结果,或者你得到了你无法匹配的结果?你有没有试过检查电线上发生了什么?也就是说,您是否尝试过使用一些网络嗅探工具或跟踪工具,或者任何可以显示传出HTTP GET请求的外观的工具?似乎您需要调试bingSearch函数的内部,而不仅仅是随机尝试外部功能。此外,您还需要指定“不成功”以外的内容。你没有得到任何结果,或者你得到了无法匹敌的结果?