Python HTML标记之间的Selenium
让Javascript创建的页面中的所有HTML传递给BeautifulSoup的最佳方式是什么 我目前正在使用:Python HTML标记之间的Selenium,python,selenium,screen-scraping,beautifulsoup,Python,Selenium,Screen Scraping,Beautifulsoup,让Javascript创建的页面中的所有HTML传递给BeautifulSoup的最佳方式是什么 我目前正在使用: from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys from BeautifulSoup import BeautifulSoup browser =
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from BeautifulSoup import BeautifulSoup
browser = webdriver.Firefox()
browser.get("http://www.yahoo.co.uk")
html = browser.find_elements_by_id("html")
但是“html”总是一个空列表。我做错了什么?HTML不是id。它应该是这样的:
html = browser.find_elements_by_tag_name("html")
因为html是一个标记
最初执行的搜索将返回id设置为“html”的所有元素。将返回的元素示例:
<p id="html">Lorem ipsum</p>
Lorem ipsum
该元素的id是“html”,标记名是“p”。您还可以使用
html_source = browser.page_source
这是一个webdriver提供的函数调用,正是为了收集完整的源代码或“获取页面中的所有html”将页面源代码从Selenium传递到Beauty Soup的正确方法是:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from BeautifulSoup import BeautifulSoup
browser = webdriver.Firefox()
browser.get("http://www.yahoo.co.uk")
html_source = browser.page_source
html = BeautifulSoup(html_source)
通过这种方式,浏览器加载页面,提取完整的html源代码并将其传递给BeautifulSoup。结果可以像任何其他Beautiful Soup对象一样进行解析。Selenium几乎完成了BeautifulSoup所做的一切——您甚至可以直接访问和操作javascript创建的对象。硒绑定与漂亮的汤绑定也没有太大区别。