Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 以编程方式打开网页,获得意外结果_Python_Html_Ruby_Nokogiri_Urllib2 - Fatal编程技术网

Python 以编程方式打开网页,获得意外结果

Python 以编程方式打开网页,获得意外结果,python,html,ruby,nokogiri,urllib2,Python,Html,Ruby,Nokogiri,Urllib2,我正在尝试从该网站获取信息: 如果你在浏览器中查看该页面,你会看到一个漂亮的,其中包含所有球员信息,下面是教练信息 当我将该页面拉入python程序(使用urllib2)或ruby程序(使用nokogiri)时,表被表示为一组div元素。我想可能有一些javascript正在运行,所以我在浏览器上禁用了javascript并重新访问了页面。它仍然在表就位的情况下加载 如果我使用Selenium拉入页面源代码,我会得到表格式 你知道为什么这个页面会有div吗 Python: page = url

我正在尝试从该网站获取信息:

如果你在浏览器中查看该页面,你会看到一个漂亮的
,其中包含所有球员信息,下面是教练信息

当我将该页面拉入python程序(使用
urllib2
)或ruby程序(使用
nokogiri
)时,表被表示为一组
div
元素。我想可能有一些javascript正在运行,所以我在浏览器上禁用了javascript并重新访问了页面。它仍然在
就位的情况下加载

如果我使用
Selenium
拉入页面源代码,我会得到
格式

你知道为什么这个页面会有div吗

Python:

page = urllib2.urlopen(url)
html = page.read()
print html
output(我将其中一个
div
s放在最后一行以引起注意。这是浏览器页面中的一个
tr
。缩短为不超过字符限制):

没有找到
tr
s,但是

doc.css('div').each do |node|
  puts node.text
end

查找
div
s

通过添加用户代理标题,我可以获得
而不是
div
s。具体来说,我假装是一个著名的流行浏览器

opener = urllib2.build_opener()
opener.addheaders = [('User-agent',
    ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) '
     'AppleWebKit/535.1 (KHTML, like Gecko) '
     'Chrome/13.0.782.13 Safari/535.1'))
]
response = opener.open('http://www.gocrimson.com/sports/mbkb/2011-12/roster')
print response.readlines() # divs are now a table

泰勒,请给我们看看你的代码,Ruby和Python,你不能指望我们从头开始编写你的脚本,也许会有不同的结果。顺便说一句,反对票不是我投的,我讨厌人们这样做而不给出理由。更新的例子。
doc.css('div').each do |node|
  puts node.text
end
opener = urllib2.build_opener()
opener.addheaders = [('User-agent',
    ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_7) '
     'AppleWebKit/535.1 (KHTML, like Gecko) '
     'Chrome/13.0.782.13 Safari/535.1'))
]
response = opener.open('http://www.gocrimson.com/sports/mbkb/2011-12/roster')
print response.readlines() # divs are now a table