擅长:python、mysql、java
<p>您可以使用BeautifulSoup查找web结果,然后查找第一个返回href的元素:</p>
<pre><code>import requests
import bs4
query = 'hello world'
url = 'http://google.com/search?q=' + query
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
page= requests.get(url, headers=headers)
soup = bs4.BeautifulSoup(page.text, 'html.parser')
for elem in soup(text='Web results'):
print (elem.find_next('a')['href'])
</code></pre>
<p><strong>输出:</strong></p>
<pre><code>print (elem.find_next('a')['href'])
https://en.wikipedia.org/wiki/%22Hello,_World!%22_program
</code></pre>