【问题标题】:Google search scraper , Python谷歌搜索爬虫,Python
【发布时间】:2017-02-25 03:36:47
【问题描述】:

我是 Python 新手,并试图制作一个 Google 搜索刮板以获取股票价格,但我在下面运行我的代码我没有得到任何结果,而是我正在获取页面 HTML 格式。

import urllib.request
from bs4 import BeautifulSoup

import requests

url = 'https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=uwti'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(html, "html.parser")

print(soup.prettify())

我是否遗漏了一些非常简单的东西,请给我一些指示。我正在尝试提取当前股票价值。如何在附图中提取此价值?

【问题讨论】:

  • 它似乎正在获取价格并动态显示它,可能使用 Javascript,因此它不会出现在您从 BeautifulSoup 收到的 HTML 中。我认为你需要采取不同的方法。有很多不错的 Python 选项可以从 Yahoo 或 Google 下载股票数据,试试看。
  • @jeffcarey,如果请求正确,它在源中,不涉及 Javascript。

标签: python parsing beautifulsoup urllib


【解决方案1】:
  1. user-agent 添加到您的请求中,以便 Google 将您的请求视为真正的用户访问,因为默认的 requests user-agentpython-requests 并且 Google 理解它并阻止请求,因此您会收到某种不同的 HTML的错误。 List of user-agentsCheck what's your user-agent
  2. 使用SelectorGadget Chrome 扩展程序通过单击浏览器中所需的元素来快速查找和获取CSS 选择器。 CSS 选择器reference
  3. 使用提取的CSS选择器使用.select_one()bs4方法获取数据。

代码和example in the online IDE

from bs4 import BeautifulSoup
import requests, lxml

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

html = requests.get('https://www.google.com/search?q=spgsclp', headers=headers)
soup = BeautifulSoup(html.text, 'lxml')

current_stock_price = soup.select_one('.wT3VGc').text
print(current_stock_price)

# 108,52

或者,您可以使用来自 SerpApi 的 Google Direct Answer Box API 来做同样的事情。这是一个带有免费计划的付费 API。

在您的案例中,最大的不同是您不必弄清楚为什么某些东西不起作用并弄清楚如何抓取这些数据。获取数据的过程更加清晰。

要集成的代码:

from serpapi import GoogleSearch

params = {
  "api_key": "YOUR_API_KEY",
  "engine": "google",
  "q": "spgsclp",
}

search = GoogleSearch(params)
results = search.get_dict()

current_stock_price = results['answer_box']['price']
print(current_stock_price)

# 108,52

免责声明,我为 SerpApi 工作。

【讨论】:

    【解决方案2】:

    谷歌不会让你刮掉它,所以你必须使用一些 API 或者只是改变股票的网站。

    import urllib
    from bs4 import BeautifulSoup
    
    url = 'siteurl'
    response = urllib.urlopen(url)
    
    soup = BeautifulSoup(response, "html.parser")
    
    print(soup.findAll("div", { "class" : 'classname' }))
    

    你可以通过改变'siteurl'和'classname'(你必须刮掉)来使用这个代码

    【讨论】:

      【解决方案3】:

      当您在浏览器中右键单击并选择查看源代码时,它就在源代码中。您只需稍微更改 url 并传递一个 user-agent 以匹配您使用请求在那里看到的内容:

      In [2]: from bs4 import BeautifulSoup
         ...: import requests
         ...: 
         ...: url = 'https://www.google.com/search?q=uwti&rct=j'
         ...: response = requests.get(url, headers={
         ...:     "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (K
         ...: HTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"})
         ...: html = response.content
         ...: 
         ...: soup = BeautifulSoup(html, "html.parser")
         ...: print(soup.select_one("span._Rnb.fmob_pr.fac-l").text)
         ...: 
      27.51
      

      soup.find("span", class_="_Rnb fmob_pr fac-l").text 也可以工作,并且是使用带有 find 或 find_all

      css 类 查找标签的正确方法

      当你使用https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=uwti时,你可以在chrome中看到,有一个重定向到https://www.google.com/search?q=uwti&rct=j

      【讨论】:

      • Padraic,您能否给我一些关于如何建立/查找您在 request.get 中提到的标头的详细信息的指示
      • @Fenomatik,当您输入 google.com/… 时,如果您在 chrome 工具中查看请求,您可以看到它在您的浏览器中变为 google.com/search?q=uwti&rct=j你可以看到这一切正在发生。发送用户代理是相当标准的,并且在抓取网站时经常需要。
      • 我尝试在 Chrome 中使用开发工具,但我找不到任何像您发现的 GET 请求?主要是 POST 请求。知道我做错了什么吗?
      • 肯定存在,确保您在 XHR 选项卡下进行检查,并在打开工具后提出请求。
      【解决方案4】:

      查看Beautiful Soup'sdocumentation,了解如何选择您刚刚解析的 HTML 文档的元素,您可以尝试以下操作:

      soup.findAll("span", ['_Rnb', 'fmob_pr, 'fac-l'])

      上面的方法会在列表中找到实现类的span元素。

      仅供参考:从我所见,初始请求并未获取股票价格,请使用浏览器的Inspect Element 功能来捕获发送的请求,从我所见有一个对 url @ 的请求987654325@。也许这是用来获取股票价格的,看看你是否可以直接向它发送请求而不是获取整个 HTML。

      【讨论】:

        猜你喜欢
        • 2013-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-09
        • 2020-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多