我正在尝试使用Python Beautiful Soup从IG Index页面中提取自动收录器(南非40)字段,但我无法检索它。
我尝试从中获取数据的网页是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(最近一次调用最后一次):文件 &#34; IGIndexDataScrape_Minute_v0.1.py&#34;,第30行,在 name = soup.select(&#39; div.ma-content title h1&#39;)[0] .text IndexError:列表索引超出范围
对上述内容的任何建议/代码更正都会非常有用。
以下是直接复制和粘贴的完整代码:
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
答案 0 :(得分:2)
您是否尝试过find_all
代替select
?类似的东西:
name_div = soup.find_all('div', {'class': 'ma-content title'})[0]
name = name_div.find('h1').text