python通过websi机制化检查日期/时间

2024-04-25 22:44:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python mechanize检查检查日期/时间的可用性,并在结果中显示特定日期/时间时发送电子邮件给某人(附结果页屏幕截图)

import mechanize
from BeautifulSoup import BeautifulSoup
URL = "http://secure.dre.ca.gov/PublicASP/CurrentExams.asp"


br = mechanize.Browser()
response = br.open(URL)


# there are some errors in doctype and hence filtering the page content a bit
response.set_data(response.get_data()[200:])

br.set_response(response)
br.select_form(name="entry_form")

# select Oakland for the 1st set of checkboxes

for i in range(0,     len(br.find_control(type="checkbox",name="cb_examSites").items)):
    if i ==2:
        br.find_control(type="checkbox",name="cb_examSites").items[i].selected =True

# select salesperson for the 2nd set of checkboxes

for i in range(0, len(br.find_control(type="checkbox",name="cb_examTypes").items)):
    if i ==1:
        br.find_control(type="checkbox",name="cb_examTypes").items[i].selected =True

reponse = br.submit()
print  reponse.read()

我可以得到响应,但由于某种原因,我的表中的数据丢失了

下面是来自初始html页面的按钮

^{pr2}$

输出(提交响应)中实际数据所在的一部分

<table summary="California Exams Scheduling" class="General_list" width="100%" cellspacing="0"> <EVERTHING INBETWEEN IS MISSING HERE>
</table>

表中的所有数据都丢失了。我提供了chrome浏览器中table元素的屏幕截图。在

  1. 有人能告诉我可能出了什么问题吗?在
  2. 有人能告诉我如何从回复中得到日期/时间(假设我必须使用BeautifulSoup),所以必须在这些行中使用。我想知道回复中的某个特定日期(比如3月8日)是否显示为下午1:30的开始时间

    汤=美食家(响应。读取()) 打印汤。找(name=“table”)


更新-看起来我的问题可能与this question有关,正在尝试我的选项。我根据其中一个答案尝试了下面的方法,但是在数据中看不到任何tr元素(尽管当我手动检查时,可以在页面源代码中看到这一点)

soup.findAll('table')[0].findAll('tr') 

enter image description here


更新-修改了这个使用selenium,将尝试在不久的某个时候做进一步的工作

from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup
import urllib3


myURL = "http://secure.dre.ca.gov/PublicASP/CurrentExams.asp"
browser = webdriver.Firefox() # Get local session of firefox
browser.get(myURL) # Load page

element = browser.find_element_by_id("Checkbox5")
element.click()


element = browser.find_element_by_id("Checkbox13")
element.click()

element = browser.find_element_by_name("B1")
element.click()

Tags: namefrombrimportbrowserforresponsetype