python - 从网页 API 中抓取数据块
问题描述
我尝试从网页中收集形成小表格的块数据。请在下面查看我的代码。
`
import requests
import re
import json
import sys
import os
import time
from lxml import html,etree
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://www.investing.com/instruments/OptionsDataAjax'
params = {'pair_id': 525, ## SPX
'date': 1536555600, ## 2018-9-4
'strike': 'all', ## all prices
'callspots': 'calls',#'call_andputs',
'type':'analysis', # webpage viewer
'bringData':'true',
}
headers = {'User-Agent': Chrome/39.0.2171.95 Safari/537.36'}
def R(text, end='\n'): print('\033[0;31m{}\033[0m'.format(text), end=end)
def G(text, end='\n'): print('\033[0;32m{}\033[0m'.format(text), end=end)
page = requests.get(url, params=params,headers = headers)
if page.status_code != 200:
R('ERROR CODE:{}'.format(page.status_code))
sys.exit
G('Problem in connection!')
else:
G('OK')
soup = BeautifulSoup(page.content,'lxml')
spdata = json.loads(soup.text)
print(spdata['data'])`
这个结果--spdata['data'] 给了我一个 str,我只想在这个 str 中获得以下块。这个str里面有很多这样格式相同的数据块。
SymbolSPY180910C00250000
Delta0.9656
Imp Vol0.2431
Bid33.26
Gamma0.0039
Theoretical33.06
Ask33.41
Theta-0.0381
Intrinsic Value33.13
Volume0
Vega0.0617
Time Value-33.13
Open Interest0
Rho0.1969
Delta / Theta-25.3172
我在这里使用 json 和 BeautifulSoup,也许正则表达式会有所帮助,但我对 re 了解不多。为了得到结果,任何方法都值得赞赏。谢谢。
解决方案
在您的代码后添加:
regex = r"((SymbolSPY[1-9]*):?\s*)(.*?)\n[^\S\n]*\n[^\S\n]*"
for match in re.finditer(regex, spdata['data'], re.MULTILINE | re.DOTALL):
for line in match.group().splitlines():
print (line.strip())
输出
OK
SymbolSPY180910C00245000
Delta0.9682
Imp Vol0.2779
Bid38.26
Gamma0.0032
Theoretical38.05
Ask38.42
Theta-0.0397
Intrinsic Value38.13
Volume0
Vega0.0579
Time Value-38.13
Open Interest0
Rho0.1934
Delta / Theta-24.3966
SymbolSPY180910P00245000
Delta-0.0262
Imp Vol0.2652
...
推荐阅读
- outlook - 如何使用 Graph API 获取电子邮件附件的嵌套附件?
- mysql - 递归选择表
- python - 如何与 Tkinter 的 while 循环并行运行代码?
- c - C中的'和'有什么区别?
- python - 调用 Python 脚本时如何消除 IIS 上的 401 错误?
- python - 如何从 CSV 文件中提取数据并导入 Python 中的字典?
- python - Unable to read some files found using os.walk
- r - 如何在R中将一个类别从列中获取到另一列?
- c# - Janusys scheduler problem, when adding a bunch of appointments
- swift - 当我通过点击创建 UIView 时,如何不让它们重叠?