soup.select('.r a') in f'https://google.com/search?q={query}' 在 Python BeautifulSoup 中带回空列表.**不是复制品** [英] soup.select('.r a') in f'https://google.com/search?q={query}' brings back empty list in Python BeautifulSoup. **NOT A DUPLICATE**
问题描述
情况:
我很幸运!"使用 Python 自动化无聊的东西"中的项目电子书不再使用他提供的代码.
The "I'm Feeling Lucky!" project in the "Automate the boring stuff with Python" ebook no longer works with the code he provided.
特别是:
linkElems = soup.select('.r a')
我做了什么:我已经尝试使用此 stackoverflow 问题
我目前也在使用相同的搜索格式.
I'm also currently using the same search format.
代码:
import webbrowser, requests, bs4
def im_feeling_lucky():
# Make search query look like Google's
search = '+'.join(input('Search Google: ').split(" "))
# Pull html from Google
print('Googling...') # display text while downloading the Google page
res = requests.get(f'https://google.com/search?q={search}&oq={search}')
res.raise_for_status()
# Retrieve top search result link
soup = bs4.BeautifulSoup(res.text, features='lxml')
# Open a browser tab for each result.
linkElems = soup.select('.r') # Returns empty list
numOpen = min(5, len(linkElems))
print('Before for loop')
for i in range(numOpen):
webbrowser.open(f'http://google.com{linkElems[i].get("href")}')
问题:
linkElems 变量返回一个空列表 [] 并且程序在此之后不会做任何事情.
The linkElems variable returns an empty list [] and the program doesn't do anything past that.
问题:
有人可以指导我正确处理这个问题,并解释为什么它不起作用吗?
Could sombody please guide me to he correct way of handling this and perhaps explain why it isn't working?
推荐答案
我在阅读那本书时也遇到了同样的问题,并找到了该问题的解决方案.
I too had had the same problem while reading that book and found a solution for that problem.
替换
soup.select('.r a')
与
soup.select('div#main > div > div > div > a')
会解决这个问题
以下是可以工作的代码
import webbrowser, requests, bs4 , sys
print('Googling...')
res = requests.get('https://google.com/search?q=' + ' '.join(sys.argv[1:]))
res.raise_for_status()
soup = bs4.BeautifulSoup(res.text)
linkElems = soup.select('div#main > div > div > div > a')
numOpen = min(5, len(linkElems))
for i in range(numOpen):
webbrowser.open('http://google.com' + linkElems[i].get("href"))
上面的代码从命令行参数中获取输入
the above code takes input from commandline arguments
这篇关于soup.select('.r a') in f'https://google.com/search?q={query}' 在 Python BeautifulSoup 中带回空列表.**不是复制品**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!