【问题标题】:Scraping a table with Python使用 Python 抓取表格
【发布时间】:2020-04-02 01:59:08
【问题描述】:

我正在尝试用 python 抓取这张表 (https://futures.tradingcharts.com/marketquotes/ZC.html)。我已经尝试过基于这个post 的东西,但是当我手动检查网站的来源时,我没有看到表格。我如何抓取这张表?

<div class="mq_page_wrapper">
 <script type="text/javascript">
    $(document).ready(function(){
      generateTCPSLink();
    });

    function generateTCPSLink(){
      var root = location.protocol + '//' + location.host;

      var url_param = {
        action:'tcps_logged_in',
        timestamp: (new Date()).getTime()
      };

      $.getJSON(root+'/widgets/footer_ajax/footer_common_functions.php?'+$.param(url_param),function(data){
        if(data.logged_in){
          $('span#tcps_link').html("Logout:<br>&nbsp;<a href='"+root+"/premium_subscriber/tcps_logout.php"+"'>Premium Subscriber</a><br>");
        }else{
          $('span#tcps_link').html("Login:<br>&nbsp;<a href='"+root+"/premium_subscriber/login_subscribe.php?premium_link"+"'>Premium Subscriber</a><br>");
        }
      });
    }
 </script>
 <div id="members_classic">
   <span id="tcps_link"></span>
 </div>

from selenium import webdriver
import time
import os
from bs4 import BeautifulSoup
chrome_path = r"C:\Users\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get('https://futures.tradingcharts.com/marketquotes/ZC.html')
time.sleep(80)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5)
html = driver.page_source
soup = BeautifulSoup(html,'html.parser')
print soup

【问题讨论】:

  • 在加载页面时打开浏览器的检查器并观看“网络”选项卡。您将看到该表实际上是使用来自另一个 URL (getQuote.json) 的数据生成的。您将需要发布您应该能够通过检查请求标头找到的相同 API 密钥。

标签: python selenium web-scraping html-table


【解决方案1】:
import requests

data = {
    'apikey': '2d8b3b803594b13e02a7dc827f4a63f8',
    'fields': 'settlement,previousClose,previousOpenInterest',
    'symbols': 'ZCY00,ZC*1,ZC*2,ZC*3,ZC*4,ZC*5,ZC*6,ZC*7,ZC*8,ZC*9,ZC*10,ZC*11,ZC*12,ZC*13,ZC*14,ZC*15,ZC*16,ZC*17,ZC*18,ZC*19,ZC*20,ZC*21,ZC*22,ZC*23,ZC*24,ZC*25,ZC*26,ZC*27,ZC*28,ZC*29,ZC*30,ZC*31,ZC*32,ZC*33,ZC*34,ZC*35,ZC*36,ZC*37,ZC*38,ZC*39,ZC*40,ZC*41,ZC*42,ZC*43,ZC*44,ZC*45,ZC*46,ZC*47,ZC*48,ZC*49,ZC*50'
}

r = requests.post(
    'https://ondemand.websol.barchart.com/getQuote.json', data=data).json()

for item in r['results']:
    print(item)

【讨论】:

  • 你怎么看getQuote.json在chrome的network标签中被调用的args?
  • @Seth 我正在使用 firefox 和请求正文中包含的数据
【解决方案2】:

这是一种将数据转换成json格式的方法:

import requests
import json

headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Accept-Language': 'en-US,en;q=0.5',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Origin': 'https://futures.tradingcharts.com',
        'Connection': 'keep-alive',
        'Referer': 'https://futures.tradingcharts.com/futures/quotes/ZC.html',
        'Cache-Control': 'max-age=0',
        'TE': 'Trailers',
    }

data = {
  'apikey': '2d8b3b803594b13e02a7dc827f4a63f8',
  'fields': 'settlement,previousClose,previousOpenInterest',
  'symbols': 'ZCY00,ZC*1,ZC*2,ZC*3,ZC*4,ZC*5,ZC*6,ZC*7,ZC*8,ZC*9,ZC*10,ZC*11,ZC*12,ZC*13,ZC*14,ZC*15,ZC*16,ZC*17,ZC*18,ZC*19,ZC*20,ZC*21,ZC*22,ZC*23,ZC*24,ZC*25,ZC*26,ZC*27,ZC*28,ZC*29,ZC*30,ZC*31,ZC*32,ZC*33,ZC*34,ZC*35,ZC*36,ZC*37,ZC*38,ZC*39,ZC*40,ZC*41,ZC*42,ZC*43,ZC*44,ZC*45,ZC*46,ZC*47,ZC*48,ZC*49,ZC*50'
}

response = requests.post('https://ondemand.websol.barchart.com/getQuote.json', headers=headers, data=data)

data = json.loads(response.text)
data['results']

你可以从那里拿走它。

【讨论】:

  • 为什么加载json 以及为什么发送headers。您不需要像 requests 那样导入 json。也用于调用API,最大年龄为零。你不需要标题。
  • @αԋɱҽԃαмєяιcαη - 谢谢;当我有更多时间时,我会研究这些要点,但基本上我只是照原样复制 API 调用。
猜你喜欢
  • 1970-01-01
  • 2016-01-31
  • 2019-10-30
  • 2020-09-08
  • 2021-01-26
  • 2020-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多