use*_*791 87 python selenium selenium-webdriver
如何使用带有Python的Selenium模块在变量中获取HTML源代码?
我想做这样的事情:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(raw_input("Enter URL: "))
if "whatever" in html_source:
# Do something
else:
# Do something else
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?我不知道如何访问HTML源代码.
Aut*_*ter 168
你需要打电话给page_source酒店.见下文.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get(raw_input("Enter URL: "))
html_source = browser.page_source
if "whatever" in html_source:
# do something
else:
# do something else
Run Code Online (Sandbox Code Playgroud)
driver.page_source将帮助您获取页面源代码。您可以检查该文本是否存在于页面源中。
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("some url")
if "your text here" in driver.page_source:
print('Found it!')
else:
print('Did not find it.')
Run Code Online (Sandbox Code Playgroud)
如果要将页面源存储在变量中,请在driver.get之后添加以下行:
var_pgsource=driver.page_source
Run Code Online (Sandbox Code Playgroud)
并将if条件更改为:
if "your text here" in var_pgsource:
Run Code Online (Sandbox Code Playgroud)
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()
html_source_code = driver.execute_script("return document.body.innerHTML;")
html_soup: BeautifulSoup = BeautifulSoup(html_source_code, 'html.parser')
Run Code Online (Sandbox Code Playgroud)
现在您可以应用 BeautifulSoup 函数来提取数据...
使用Selenium2Library,您可以使用 get_source()
import Selenium2Library
s = Selenium2Library.Selenium2Library()
s.open_browser("localhost:7080", "firefox")
source = s.get_source()
Run Code Online (Sandbox Code Playgroud)
Gri*_*fin -7
我建议使用urllib获取源代码,如果您要解析,请使用Beautiful Soup之类的东西。
import urllib
url = urllib.urlopen("http://example.com") # Open the URL.
content = url.readlines() # Read the source and save it to a variable.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
122551 次 |
| 最近记录: |