如何等待页面加载完成? [英] How to wait for page load to complete?
问题描述
我尝试了下面的代码,但它总是在获得大小之前返回.
I tried below code, but it always returned before the size is got.
# config.url = 'http://www.neimanmarcus.com/Stuart-Weitzman-Reserve-Suede-Over-the-Knee-Boot-Black/prod179890262/p.prod'
import urllib2
import requests
import config
import time
from lxml.cssselect import CSSSelector
from lxml.html import fromstring
print config.url
headers = {
"Host": "www.neimanmarcus.com",
"Connection": "keep-alive",
"Content-Length": 106,
"Pragma": "no-cache",
"Cache-Control": "no-cache",
"Accept": "*/*",
"Origin": "http://www.neimanmarcus.com",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Referer": "http://www.neimanmarcus.com/Stuart-Weitzman-Reserve-Suede-Over-the-Knee-Boot-Black/prod179890262/p.prod",
"Accept-Language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,fr;q=0.2,cs;q=0.2,zh-TW;q=0.2"
}
request = urllib2.Request(config.url, headers=headers)
html = urllib2.urlopen(request)
time.sleep(10)
html = html.read()
print html
html = fromstring(html)
sel = CSSSelector('option.addedOption')
try:
options = sel(html)
print options
except Exception as e:
print e
我发现尺寸是在请求 'http://www.neimanmarcus.com/product.service'(其实Header是根据这个请求的请求头创建的)
I found size is got in a request 'http://www.neimanmarcus.com/product.service' (actually the Header is created according to the request header of this request).
如何获取整个页面信息(尤其是引导大小)?
How can I get the whole page information (especially with the boot size)?
我也尝试请求 http://www.neimanmarcus.com/product.service直接但也失败了.
I also tried to request http://www.neimanmarcus.com/product.service directly but failed as well.
推荐答案
我的理解是正确的:不管代码休眠多久还是没有加载鞋码?
As I understand correctly: no matter how long the code sleeps it still hasn't loaded the shoe size?
由于您没有使用无头浏览器,因此您不会在请求的页面上执行 javascript.尝试使用像 PhantomJS 这样的无头浏览器.此处列出了更多无头浏览器.
Since you are not using a headless browser you do not execute javascript on the requested page. Try using a headless browser like PhantomJS. Here a list of more headless browsers.
这里是如何使用 PhantomJS 在 Python 中的一种方法一>.
这篇关于如何等待页面加载完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!