使用 Mechanize Python 隐藏 HTML 个元素
Hidden HTML elements using Mechanize Python
所以我正在编写一个 Python 脚本来检查 Blackboard(学校界面站点)的更新。但是我从我的脚本收到的 HTML 与在我的浏览器中查看时的 HTML 并不完全相同。我不确定这是 cookie 问题还是我遗漏了什么。
USERNAME = ''
PASSWORD = ''
updates = 0
site = 'http://schoolsite.edu'
browser = mechanize.Browser()
browser.open(site)
browser.select_form(nr = 0)
browser.form['j_username'] = USERNAME
browser.form['j_password'] = PASSWORD
browser.submit()
#it brings back an empty form, just submit it.
browser.select_form(nr = 0)
browser.submit()
html_resp = browser.response().read()
有问题的 HTML 看起来像这样(这是来自脚本)
<span id="badgeTotal" style="visibility: hidden" title="">
<span class="hideoff" id="badgeAXLabel">Activity Updates</span>
<span class="badge" id="badgeTotalCount" title=""></span>
我期待的样子(来自 Chrome/actual 浏览器)
<span id="badgeTotal" style="visibility: visible;" title="">
<span class="hideoff" id="badgeAXLabel">Activity Updates</span>
<span class="badge" id="badgeTotalCount" title="">1</span>
我真正想要的是最后一行中的数字“1”,但我觉得可见性属性阻碍了它。请注意,我从 Mechanize 获得的 cookie 与我在浏览器中获得的 cookie 相同。 (不完全相同,但 ID、名称等相同)
有什么想法吗?
欢迎任何意见。
很确定有 javascript 涉及 which mechanize cannot handle.
此处的另一种解决方案是通过 selenium
:
使真实浏览器自动化
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox() # could also be headless: webdriver.PhantomJS()
driver.get('http://schoolsite.edu')
# submit a login form
username = driver.find_element_by_name('j_username')
password = driver.find_element_by_name('j_password')
username.send_keys(USERNAME)
password.send_keys(PASSWORD)
username.submit()
# wait for the badge count to appear
badge_count = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "badgeTotalCount")))
print(badge_count.text)
所以我正在编写一个 Python 脚本来检查 Blackboard(学校界面站点)的更新。但是我从我的脚本收到的 HTML 与在我的浏览器中查看时的 HTML 并不完全相同。我不确定这是 cookie 问题还是我遗漏了什么。
USERNAME = ''
PASSWORD = ''
updates = 0
site = 'http://schoolsite.edu'
browser = mechanize.Browser()
browser.open(site)
browser.select_form(nr = 0)
browser.form['j_username'] = USERNAME
browser.form['j_password'] = PASSWORD
browser.submit()
#it brings back an empty form, just submit it.
browser.select_form(nr = 0)
browser.submit()
html_resp = browser.response().read()
有问题的 HTML 看起来像这样(这是来自脚本)
<span id="badgeTotal" style="visibility: hidden" title="">
<span class="hideoff" id="badgeAXLabel">Activity Updates</span>
<span class="badge" id="badgeTotalCount" title=""></span>
我期待的样子(来自 Chrome/actual 浏览器)
<span id="badgeTotal" style="visibility: visible;" title="">
<span class="hideoff" id="badgeAXLabel">Activity Updates</span>
<span class="badge" id="badgeTotalCount" title="">1</span>
我真正想要的是最后一行中的数字“1”,但我觉得可见性属性阻碍了它。请注意,我从 Mechanize 获得的 cookie 与我在浏览器中获得的 cookie 相同。 (不完全相同,但 ID、名称等相同)
有什么想法吗?
欢迎任何意见。
很确定有 javascript 涉及 which mechanize cannot handle.
此处的另一种解决方案是通过 selenium
:
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox() # could also be headless: webdriver.PhantomJS()
driver.get('http://schoolsite.edu')
# submit a login form
username = driver.find_element_by_name('j_username')
password = driver.find_element_by_name('j_password')
username.send_keys(USERNAME)
password.send_keys(PASSWORD)
username.submit()
# wait for the badge count to appear
badge_count = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "badgeTotalCount")))
print(badge_count.text)