使用urllib进行Web刮擦

问题描述 投票:0回答:1

我希望从CME website获得一些信息。即我想获得10年国库券期货的期货收益率和期货DV01。在旧的thread上找到这个小片段:

import urllib.request
class AppURLopener(urllib.request.FancyURLopener):
    version = "Mozilla/5.0"
opener = AppURLopener()
fh = opener.open('http://www.cmegroup.com/tools-information/quikstrike/treasury-analytics.html')

它抛出了弃用警告,我不太确定如何从网站获取信息。有人可以告诉我新的语法应该是什么以及如何获取信息。谢谢

python web-scraping python-3.6 urllib
1个回答
2
投票

完成安装selenium后运行脚本。

from selenium import webdriver ; from bs4 import BeautifulSoup

driver = webdriver.Chrome()
driver.get("http://www.cmegroup.com/tools-information/quikstrike/treasury-analytics.html")

driver.switch_to_frame(driver.find_element_by_tag_name("iframe"))
soup = BeautifulSoup(driver.page_source, 'html.parser')
driver.quit()

table = soup.select('table.grid')[0]
list_of_rows = [[t_data.text for t_data in item.select('th,td')]
                for item in table.select('tr')]

for data in list_of_rows:
    print(data)

我想,这是你所追求的表[部分图片]:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.