【发布时间】:2018-12-12 00:43:29
【问题描述】:
我正在尝试从特定的门户网站抓取网络数据。我之前尝试过学习和实验,但使用 beautiful_soup 和 urllib 的成功有限。
下面是我的代码,它似乎没有抓取我需要的数据...
httpLoc = 'https://uk.investing.com/currencies/forex-options'
url = requests.get(httpLoc,headers={'User-Agent': 'Mozilla/5.0'})
fx_data = np.array([])
content_page = soup(url.content,'html.parser')
containers = content_page.findAll('table', {'class':'vol-data-col'})
for table in containers:
for td in table.findAll('vol-data-col'):
#print(td.text)
fx_data = np.append(fx_data, td.text)
网站中的 html 代码格式如下。我正在尝试迭代提取所有形式为“14.77”的行
td class="vol-data-col ng-binding ng-scope" ng-mouseover="PageSettings.setHoverInstrumentTitle(instruments[$parent.$index].title)" ng-mouseleave="PageSettings.clearHoverInstrumentTitle(instruments[$parent.$index].title)" ng-repeat="period in periods" ui-sref="currency" ng-click="PageSettings.clearHoverInstrumentTitle(); $parent.$parent.$parent.currentTenor = period.name; summaryClickFunc(period, instruments[$parent.$index]); periods[$index].active = true">14.77%</td>
所附图片是数据在网站上的样子
----从 cmets 更新----
我开始尝试使用 selenium,这就是我所拥有的:
import os from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\\Python\\chromedriver.exe")
# Initialize the webdriver session
driver.get('https://uk.investing.com/currencies/forex-options')
# replaces "ie.navigate"
test = driver.find_elements_by_xpath(("//*[@id='curr_table']/class"))
【问题讨论】:
-
如果你检查
url.content或content_page,你能看到table的数据吗? -
您是否尝试过使用 pd.read_html(url)
-
如果所有相关表都具有相同的类,请尝试在 findAll 中包含整个类字符串:
"vol-data-col ng-binding ng-scope" -
@alecxe,是的,我做到了...如果我检查 content_page,我可以看到整个 html 页面/代码已加载。
-
@alexce,不...使用 html5lib.parser 不起作用。我不认为这是问题所在。
标签: python-3.x web-scraping beautifulsoup urllib