我正在使用python学习网页抓取。
这是我的第一个python代码
# encoding=utf8
import urllib2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen("http://www.bcsfootball.org/").read(),"lxml")
for row in soup("table", {'class': "mod-data"})[0].tbody("tr"):
tds = row('td')
print tds[0].string, tds[1].string
我收到错误
/usr/bin/python2.7 /home/NewYork/PycharmProjects/untitled/News.py
Traceback (most recent call last):
File "/home/NewYork/PycharmProjects/untitled/News.py", line 8, in <module>
for row in soup("table", {'class': "mod-data"})[0].tbody("tr"):
IndexError: list index out of range
任何人都可以帮助我做错了吗?
还有一件事我想问一下......请帮助我理解这里发生的事情......
for row in soup("table", {'class': "mod-data"})[0].tbody("tr"):
谢谢!! :)
答案 0 :(得分:1)
错误消息表示var messageTemplate = $("#messageTemplate").clone()
.removeAttr('id').removeClass("hidden").html();
var timeStamp = message.TimeStamp;
console.log(timeStamp)
var relativeTime = moment(timeStamp, "YYYYMMDD").fromNow();
$(messageTemplate).find(".message").html(message.Data)
$(messageTemplate).find(".moment").html(relativeTime);
$(messageTemplate).find(".senderName").html(message.SenderUser.Username);
$("#message-dropdown").prepend(messageTemplate);
是一个空列表,但您想获取此列表中的第一个元素。
您应确保soup("table", {'class': "mod-data"})
元素具有使用类table
的节点。
答案 1 :(得分:0)
这会给你预期的结果:
import urllib2
from bs4 import BeautifulSoup
soup = BeautifulSoup(urllib2.urlopen("http://www.bcsfootball.org").read(),"html")
welcome = soup("div", {'class': "col-full"})[1] # we know it's index 1
for item in welcome:
print item.string