gpt4 book ai didi

python - 如何等待页面加载完成?

转载 作者:行者123 更新时间:2023-11-30 22:56:07 26 4
gpt4 key购买 nike

我正在尝试从 http://www.neimanmarcus.com/Stuart-Weitzman-Reserve-Suede-Over-the-Knee-Boot-Black/prod179890262/p.prod 获取可用的启动大小(在 $('option.addedOption') 下)

我尝试了下面的代码,但它总是在获取大小之前返回。

# 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是根据本次请求的请求头创建的)。

如何获取整个页面信息(尤其是启动大小)?

我也尝试请求http://www.neimanmarcus.com/product.service直接但也失败了。

最佳答案

据我理解正确:无论代码休眠多长时间,它仍然没有加载鞋子尺码?

由于您没有使用 headless 浏览器,因此您不会在请求的页面上执行 javascript。尝试使用 headless 浏览器,例如 PhantomJS 。这里有更多的列表headless browsers .

这是一种如何使用 PhantomJS in Python 的方法.

关于python - 如何等待页面加载完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37138085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com