我正在使用Linux平台上的splinter 0.7.3模块在python 2.7.2中使用默认的火狐浏览器在网站上刮取目录列表。
这是通过单击html中的“Next”链接来遍历分页web列表的代码片段。
links = True
i = 0
while links:
with open('html/register_%03d.html' % i, 'w') as f:
f.write(browser.html.encode('utf-8'))
links = browser.find_link_by_text('Next')
print 'links:', links
if links:
links[0].click()
i += 1我知道链接正在工作,因为我看到的输出如下:
links: [<splinter.driver.webdriver.WebDriverElement object at 0x2e6da10>, <splinter.driver.webdriver.WebDriverElement object at 0x2e6d710>]
links: [<splinter.driver.webdriver.WebDriverElement object at 0x2e6d5d0>, <splinter.driver.webdriver.WebDriverElement object at 0x2e6d950>]
links: [<splinter.driver.webdriver.WebDriverElement object at 0x2e6d710>, <splinter.driver.webdriver.WebDriverElement object at 0x2e6dcd0>]
links: []当使用f.write(browser.html.encode('utf-8'))在每个页面上保存html时,它对第一个页面很好。在随后的页面中,虽然我可以看到在Firefox中呈现的页面,但是html/regiser_...html文件是空的,或者body标记丢失了,如下所示:
<!DOCTYPE html>
<!--[if lt IE 7]> <html prefix="og: http://ogp.me/ns#" class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en-gb"> <![endif]-->
<!--[if IE 7]> <html prefix="og: http://ogp.me/ns#" class="no-js lt-ie9 lt-ie8" lang="en-gb"> <![endif]-->
<!--[if IE 8]> <html prefix="og: http://ogp.me/ns#" class="no-js lt-ie9" lang="en-gb"> <![endif]-->
<!--[if gt IE 8]><!-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-gb" class="no-js" prefix="og: http://ogp.me/ns#"><!--<![endif]--><head>
<meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible" />
...
</style>
<script src="/media/com_magebridge/js/frototype.min.js" type="text/javascript"></script></head></html>这是从碎片中保存html的一个已知特性吗?有更好的方法吗?
发布于 2016-01-01 06:04:47
这看起来确实是一个时间问题-当页面未被完全加载时,您将获得页面源。有几种方法可以解决这一问题:
body 出席:
browser.is_element_present_by_tag("body",wait_time=5)browser对象之后将其放在右:
browser.driver.set_page_load_timeout( 10 ) #10秒https://stackoverflow.com/questions/32640569
复制相似问题