2016-01-26 60 views
-1

代码循环浏览页面,但并未完全遍历整个页面,通常会在比赛的会议中停止在第6或第7场比赛。任何人都可以提出BS为什么会在这里失败。是url http://www.gbgb.org.uk/resultsMeeting.aspx?id=135549无法抓取整个页面

from urllib import urlopen 
from bs4 import BeautifulSoup 
baseURL = 'http://www.gbgb.org.uk/resultsMeeting.aspx?id=135549' 
html = urlopen(baseURL) 
bsObj = BeautifulSoup(html, 'lxml') 

nameList = bsObj.findAll("div", {"class": "resultsBlockHeader"}) 
for i in nameList: 


nameList1 = i.findAll("div", {"class": "track"}) 
for j in nameList1: 
    print(j.get_text()) 

nameList1 = i.findAll("div", {"class": "date"}) 
for j in nameList1: 
    print(j.get_text()) 

nameList1 = i.findAll("div", {"class": "datetime"}) 
for j in nameList1: 
    print(j.get_text()) 

nameList1 = i.findAll("div", {"class": "grade"}) 
for j in nameList1: 
    print(j.get_text()) 

nameList1 = i.findAll("div", {"class": "distance"}) 
for j in nameList1: 
    print(j.get_text()) 

nameList1 = i.findAll("div", {"class": "prizes"}) 
for j in nameList1: 
    print(j.get_text()) 

nameList = bsObj.findAll("div", {"class": "resultsBlock"}) 
for i in nameList: 

nameList2 = i.findAll("li", {"class": "trap"}) 
for j in nameList2: 
    print(j.get_text()) 

nameList2 = i.findAll("li", {"class": "first essential fin"}) 
for j in nameList2: 
    print(j.get_text()) 

nameList2 = i.findAll("li", {"class": "essential greyhound"}) 
for j in nameList2: 
    print(j.get_text()) 

nameList2 = i.findAll("li", {"class": "sp"}) 
for j in nameList2: 
    print(j.get_text()) 

nameList2 = i.findAll("li", {"class": "timeSec"}) 
for j in nameList2: 
    print(j.get_text()) 

nameList2 = i.findAll("li", {"class": "timeDistance"}) 
for j in nameList2: 
    print(j.get_text()) 

蟒蛇网络刮BS4

+0

问题是与您的连接性,尝试使用更高的速度互联网 –

回答

0

我不能让你的代码工作作为描绘。然而,使用

from urllib.request import urlopen 

我能够在修复某些缩进之后运行其余代码就好了。请注意0​​。

当我使用浏览器时,我可以在网页上看到13个比赛结果,并且我可以使用您的BS代码获得13场比赛结果。

您的BS代码正在工作。所以我们只需要在调用BS之前查看任何问题。并且唯一可能的线路导致问题是html = urlopen(baseURL)。也许你正在处理一些连接问题。

我猜你是双重检查网页上公开呈现的内容,对吗?我怀疑随着时间的推移,比赛的数量会有所不同,因此获得6或7个结果可能就是所有的结果。

+0

嗨hfuhruhurr.Many感谢您的答复,我认为你一定是正确的问题上涨,但是当我用你的urllib代码替换上述代码我得到“导入错误没有模块命名请求”。当我看着pycharms设置它告诉我,请求已安装,所以我尝试了笔记本++,但得到了相同的消息。我要重新安装请求,有没有其他建议感谢Moon – moonshadow

+0

我会原材料来推断这个问题。也就是说,只需从终端窗口启动一个python会话即可。从urllib.request输入'urlopen'并打回车。如果你没有问题,那么这是你的PyCharms/Notepad ++设置。 (注意:'urlopen'功能的代码位于你的安装目录中的一个名为“request.py”的文件中,如果没有它,你不能使用'urlopen'。) – hfuhruhurr

+0

任何机会,你的代码文件名为“urllib py“为?如果你使用“csv”模块的话,我发现你不应该使用“csv.py”作为文件名。我猜Python认为你的csv.py是被调用的csv模块。 – hfuhruhurr

0

修复几个小缩进问题后,该代码适用于我。