【问题标题】:Page Request not loading fully页面请求未完全加载
【发布时间】:2015-10-07 13:30:22
【问题描述】:

我的请求有问题。当我得到页面时,没有得到我需要的一个特定数据。

from bs4 import BeautifulSoup    
import requests

def getHTML(url):
    headers = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/21.0'}
    r = requests.get(url, headers=headers, allow_redirects=True, timeout=None)
    #print r.headers
    soup = BeautifulSoup(r.content, 'html.parser')
    return soup


def main():
    source = getHTML('http://connected2.me')
    for link in source.find_all('ul'):
        print(link)


if __name__ == '__main__':
    main()

我需要从源中获取的数据在这个列表中

<ul class="usersOnlineList clear"></ul>

但结果总是这样

<ul class="usersOnlineList clear">
</ul>
<ul>
<li><input class="inptIcn icnUser" data-validate="validate(required, username, minlength(3), maxlength(25))" id="fos_user_registration_form_username" maxlength="255" name="fos_user_registration_form[username]" pattern=".{3,255}" placeholder="username" required="required" type="text" value=""/></li>
<li><input class="inptIcn icnPass" data-validate="validate(required, minlength(6))" id="fos_user_registration_form_plainPassword" name="fos_user_registration_form[plainPassword]" placeholder="password" required="required" type="password" value=""/></li>
<li><input class="inptIcn icnEmail" data-validate="validate(required, email)" id="fos_user_registration_form_email" maxlength="255" name="fos_user_registration_form[email]" pattern=".{2,255}" placeholder="email" required="required" type="email" value=""/></li>
<li class="formActions"><input class="btn btnGreen" id="signup-btn" name="signup-btn" type="submit" value="Sign Up!"/></li>
</ul>

列表为空。为什么我不能只从该列表中获取列表项?

【问题讨论】:

  • 您尝试打印文本了吗? print link.text
  • 没关系,文档中不存在的数据。该数据是动态加载的。

标签: python beautifulsoup python-requests


【解决方案1】:

这是因为&lt;ul class="usersOnlineList clear"&gt;&lt;/ul&gt; 的内容是JavaScript 注入的。您必须等待它被插入,requests 无法执行此操作。 selenium 可以解决这个问题:

from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Firefox()
driver.set_page_load_timeout(5)

def main():
    driver.get("http://connected2.me")
    source = BeautifulSoup(driver.page_source, 'html.parser')

    for link in source.find_all('ul', {'class': 'usersOnlineList'}):
        print(link)

    driver.close()

if __name__ == '__main__':
    main()

【讨论】:

  • 你的方法很有魅力,谢谢!有没有办法不打开我的浏览器,我的意思是隐藏?
猜你喜欢
  • 2014-03-25
  • 2014-01-17
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多