python - 使用 R/Python 从 Web 下载数据的非硒方式
问题描述
我正在寻找一些非 Selenium 方法来使用R
(最好)或Python
.
在 RI 中使用下面的代码来做同样的事情 -
library(rvest)
library(XML)
Link = 'https://www.bseindia.com/stock-share-price/itc-ltd/itc/500875/'
read_html(Link) %>% html_nodes(".textvalue .ng-binding") %>% html_text()
## character(0)
理想情况下,我应该能够获得大部分数值。但正如您所见,它无法下载任何内容。任何指向正确方法的指针都将非常有益。
我也尝试过BeautifulSoup module
如下Python
没有任何成功-
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
uClient = uReq("https://www.bseindia.com/stock-share-price/itc-ltd/itc/500875/")
page_html = uClient.read()
page_soup = soup(page_html, 'html.parser')
page_soup.findAll("div", {"class":"textvalue.ng-binding"})
谢谢,
解决方案
这很容易,因为您可以使用页面使用的 API。返回 json 包含所有值,但我只打印一个。
Python:
import requests
r = requests.get('https://api.bseindia.com/BseIndiaAPI/api/StockTrading/w?flag="etype=EQ&scripcode=500875').json()
print(r['MktCapFF'])
回复:
library(rvest)
library(jsonlite)
r <- read_html('https://api.bseindia.com/BseIndiaAPI/api/StockTrading/w?flag="etype=EQ&scripcode=500875') %>%html_text() %>%jsonlite::fromJSON(.)
print(r$MktCapFull)
推荐阅读
- c# - 重写 Text.text 会表现得很奇怪
- character-encoding - Visual Studio 2017 - 对打开的每个文件强制文件编码
- java - 尝试在空对象引用上调用虚拟方法“...(java.util.ArrayList)”
- react-native - 从生产模式中删除日志不起作用-本机反应
- spring-cloud-dataflow - 普罗米修斯集群角色绑定的 SA 主题名称错误?
- css - 如何在flexbox中将内容对齐到中心
- python - 给定一个列表,如何找到两边都被较小值包围的值
- ludwig - Ludwig 如何对图像进行编码
- django - 在 Django 应用程序中进行矩阵样式报告的最佳方法是什么?
- javascript - getHours() 在晚上 9 点返回 20,然后在 DST 返回 21 之后。如何始终返回相同的数字