使用脚本搜索必应结果会导致编码问题

2024-10-01 11:21:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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(match[0])(其中打印match[0]产生{}),我得到的结果不准确。在

我试过str(match[0])match[0].encode(encoding="UTF-8"),但是没有成功。在

有什么想法吗?在


Tags: textinforsearchmatchline单词word