我想從IG索引頁使用Python美麗的湯提取股票代碼(南非40)字段,但我無法檢索它。使用Python的網頁抓取數據美麗的湯 - 不能提取字段
我想獲得的數據由是https://www.ig.com/uk/ig-indices/south-africa-40?siteId=igm
HTML代碼與股票數據的網頁:
<div class="ma-content title">
<h1>South Africa 40</h1>
<p>
.........some text..........
</p>
</div>
我已經試過這樣:
name = soup.select('div.ma-content title h1')[0].text
,但得到的錯誤信息:
Traceback (most recent call last): File "IGIndexDataScrape_Minute_v0.1.py", line 30, in name = soup.select('div.ma-content title h1')[0].text IndexError: list index out of range
以上任何建議/代碼修正都會非常有幫助。
這裏是直複製和粘貼完整代碼:
import urllib2
from bs4 import BeautifulSoup
import csv
from datetime import datetime
from lxml import html
import requests
quote_page = ['https://www.ig.com/uk/ig-indices/south-africa-40?siteId=igm']
data = []
for pg in quote_page:
page = urllib2.urlopen(pg)
soup = BeautifulSoup(page, 'html.parser')
name = soup.select('div.ma-content title h1')[0].text
sell_price = soup.find('span', attrs={'class':'price', 'id':'bid'}).text
data.append(sell_price)
buy_price = soup.find('span', attrs={'class':'price', 'id':'ofr'}).text
data.append(buy_price)
print sell_price + "\t\t" + buy_price + name
# data.append(name, sell_price, buy_price)
# print name + "\t\t" + sell_price + "\t\t" + buy_price
請編輯您的帖子並正確格式化代碼:https://stackoverflow.com/editing-help#code –
css是錯誤的。它應該是'div.ma-content.title'或只是'div.title' – pguardiario