2016-01-31 36 views
1

刮表在这第一个代码,我可以用BS获取感兴趣的表中的所有信息:与BeautifulSoup

from urllib import urlopen 
from bs4 import BeautifulSoup 

html = urlopen("http://www.pythonscraping.com/pages/page3.html") 
soup = BeautifulSoup(html) 

for i in soup.find("table",{"id":"giftList"}).children: 
    print child 

,打印的产品清单。

我想在tournamentTablehere打印的行(所需的信息是class=deactivate,在class=center nob-borderclass=odd deactivate和日期):

from urllib import urlopen 
from bs4 import BeautifulSoup 

html = urlopen("http://www.oddsportal.com/hockey/russia/khl/results/#/page/2.html") 
soup = BeautifulSoup(html) 

#for i in soup.find("table",{"id":"tournamentTable"}).children: 
# print i 
for i in soup.find("table",{"class":"table-main"}).children: 
    print i 

但这种情况正在打印页面上的其他表。当我尝试使用{"id":"tournamentTable"}指定感兴趣的表时,它将返回Nonetype

我错过了什么,我无法访问所需的表&的信息?

+0

也许这部分是由JavaScript创建的 - urllib/bs不支持javascript。 – furas

回答

3

urllib.urlopenurllib.urlopen返回网页的内容时,它将返回来自URL的HTML,其中包含JavaScript 已关闭。在你的情况下,这意味着当urllib加载相关的URL时,id="tournamentTable"表不会实际加载。

您可以通过在浏览器中关闭JavaScript并加载URL来观察此行为。

要抓取包含JavaScript呈现内容的网页,您可能需要考虑使用浏览器自动化程序包,例如Selenium。如果你经常刮脸,你可能还想下载一个'JavaScript切换器'插件,它允许你轻松地打开和关闭JavaScript。