3

谷歌搜索给了我以下关于 HTML 的第一个结果:

<h3 class="r"><a href="https://rads.stackoverflow.com/amzn/click/com/0470284889" rel="nofollow noreferrer" class="l vst" onmousedown="return rwt(this,'','','','1','AFQjCNEv1W9YC2jcSKYdEo2kNqBMJ-Utmg','k89K9hF4cVNpxQYHtEKiUQ','0CCoQFjAA',null,event)"><em>Quantitative Trading</em>: <em>How to Build Your Own Algorithmic</em> <b>...</b> - Amazon</a></h3>

我想从中提取链接http://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889,但是当我用美汤提取信息时,我得到

soup.find("h3").find("a").get("href")

我改为获得以下字符串:

/url?q= http://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889&sa=U&ei=P2ycT6OoNuasiAL2ncV5&ved=0CBIQFjAA&usg=AFQjCNEo_ujANAKnjheWDRlBKnJ1BGeA7A

我知道链接在那里,我可以通过删除 /url?q= 和 & 符号后面的所有内容来解析它,但我想知道是否有更清洁的解决方案。

谢谢!

4

2 回答 2

1

您可以使用urlparse.urlparseurlparse.parse_qs的组合,例如

>>> import urlparse
>>> url = '/url?q=http://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889&sa=U&ei=P2ycT6OoNuasiAL2ncV5&ved=0CBIQFjAA&usg=AFQjCNEo_ujANAKnjheWDRlBKnJ1BGe'
>>> data = urlparse.parse_qs(
...     urlparse.urlparse(url).query
... )
>>> data
{'ei': ['P2ycT6OoNuasiAL2ncV5'],
 'q': ['http://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889'],
 'sa': ['U'],
 'usg': ['AFQjCNEo_ujANAKnjheWDRlBKnJ1BGe'],
 'ved': ['0CBIQFjAA']}
>>> data['q'][0]
'http://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889'
于 2012-04-28T22:41:49.727 回答
1

要仅从页面中提取第一个结果,您可以select_one()通过传递 CSS 选择器或方法来使用。find() bs4

在线IDE中的代码和示例:

import requests, lxml
from bs4 import BeautifulSoup

headers = {
    "User-Agent":
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3538.102 Safari/537.36 Edge/18.19582"
}

# passing parameters in URLs
# https://docs.python-requests.org/en/master/user/quickstart/#passing-parameters-in-urls
params = {'q': 'Quantitative Trading How to Build Your Own Algorithmic - amazon'}

def bs4_get_first_googlesearch():
    html = requests.get('https://www.google.com/search', headers=headers, params=params).text
    soup = BeautifulSoup(html, 'lxml')

    first_link = soup.select_one('.yuRUbf').a['href']
    print(first_link)

bs4_get_first_googlesearch()

# output:
'''
https://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889
'''

或者,您可以使用来自 SerpApi的Google 搜索引擎结果 API执行相同的操作。这是一个付费 API,可免费试用 5,000 次搜索。看看操场

最大的不同是所有的事情都已经为最终用户完成了:选择元素、绕过阻塞、代理轮换等等。

要集成的代码:

from serpapi import GoogleSearch
import os

def serpapi_get_first_googlesearch():
    params = {
      "api_key": os.getenv("API_KEY"),
      "engine": "google",
      "q": "Quantitative Trading How to Build Your Own Algorithmic - amazon",
      "hl": "en",
    }

    search = GoogleSearch(params)
    results = search.get_dict()
    # [0] - first element from the search results
    first_link = results['organic_results'][0]['link']
    print(first_link)

serpapi_get_first_googlesearch()

# output:
'''
https://www.amazon.com/Quantitative-Trading-Build-Algorithmic-Business/dp/0470284889
'''

免责声明,我为 SerpApi 工作。

于 2021-06-25T07:45:31.853 回答