【问题标题】:beautifulsoup: Unable to get correct info from dividendinvestor with cookiebeautifulsoup:无法从带有 cookie 的股息投资者那里获得正确的信息
【发布时间】:2021-08-29 12:17:56
【问题描述】:

我正在尝试从股息投资者.com 获取一些数据。但内容结果并没有“连续分红”等信息。

有人可以解决这个问题吗?

import requests
from bs4 import BeautifulSoup

url = 'https://www.dividendinvestor.com/dividend-quote/intc/'
headers={
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0',
    'Connection': 'keep-alive',
    'Referer': f'{url}',
    'Cookie': 'seenslideup=1; seenpopup=1',
    'TE': 'Trailers'
}

print(str(BeautifulSoup(r_get.content, 'html.parser'))

【问题讨论】:

  • 你想从URL中提取什么,请你说清楚!
  • 您好,我想了解“连续增加股息”等信息。
  • URL 动态生成数据,因此您无法使用 bs4 抓取数据,但从网络选项卡中我发现它是通过 ajax URL 呈现的,您可以调用此 URL 但您需要 @987654324 @根据需要获取数据
  • 我可以获取 url 与 bs4 一起用于其他网站,例如 dividata 和 finviz,但不能用于股息投资。如果我可以在内容中看到我的数据,我可以使用正则表达式。
  • 我已经发布了我的答案,如何使用加载数据的 URL 在 URL 中查找内容

标签: python html beautifulsoup


【解决方案1】:

您可以从返回 json 的“网络”选项卡获取 ajax 请求 URL 您可以将数据解析为 bs4 并返回 HTML,以便您提取 需要什么数据!

import requests 
from bs4 import BeautifulSoup

headers={"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"}
res=requests.get("https://www.dividendinvestor.com/ajax/?action=quote_ajax&type=POST&nonce=ea2c75061e&symbol=INTC&user_type=visitor",headers=headers)


import json
data=json.loads(res.text)['html']
soup=BeautifulSoup(data,"lxml")

输出:

<html><body><h1>INTC:Intel Corp - Stock Price Quote and Dividend Data</h1>
<div id="overview">
<div id="overview-close">
<h2>INTC STOCK PRICE LATEST CLOSE</h2>
....

【讨论】:

  • 请问您是如何从 html 中获得“dividendinvestor.com/ajax/…”的?我试图通过检查找到它,但我似乎找不到它。
  • Go to Chrom developer Mode -> Network Tab -> Relaod your website and than in the left hand site search will be there you can search element that you are looking for it will return link and you can click on那 -> 去预览 -> 复制 URL -> 并使用 python 就是这样!
  • 嗨 Bhavya,上述方法本周失败,res.text 返回 '{"error":"Nonce 验证失败!"}',我从 Network Tab 中检查了 URL,nouce 已改为dividendinvestor.com/ajax/…,但是这个url还是返回Nouce验证失败。你碰巧知道发生了什么吗?
猜你喜欢
  • 1970-01-01
  • 2022-09-25
  • 2011-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多