使用 urllib 进行网页抓取
Webscraping with urllib
我想从 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')
它会发出弃用警告,我不太确定我是如何从网站上获取信息的。有人可以启发我新语法应该是什么以及如何获取信息。谢谢
运行 安装完 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)
我想,这就是你想要的table[部分图片]:
我想从 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')
它会发出弃用警告,我不太确定我是如何从网站上获取信息的。有人可以启发我新语法应该是什么以及如何获取信息。谢谢
运行 安装完 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)
我想,这就是你想要的table[部分图片]: