我正在运行一个刮板这个课程网站,我想知道是否有一个更快的方式刮板页一旦我把它放入美化组。这比我预料的要长得多。
小费?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
driver = webdriver.PhantomJS()
driver.implicitly_wait(10) # seconds
driver.get("https://acadinfo.wustl.edu/Courselistings/Semester/Search.aspx")
select = Select(driver.find_element_by_name("ctl00$Body$ddlSchool"))
parsedClasses = {}
for i in range(len(select.options)):
print i
select = Select(driver.find_element_by_name("ctl00$Body$ddlSchool"))
select.options[i].click()
upperLevelClassButton = driver.find_element_by_id("Body_Level500")
upperLevelClassButton.click()
driver.find_element_by_name("ctl00$Body$ctl15").click()
soup = BeautifulSoup(driver.page_source, "lxml")
courses = soup.select(".CrsOpen")
for course in courses:
courseName = course.find_next(class_="ResultTable")["id"][13:]
parsedClasses[courseName] = []
print courseName
for section in course.select(".SecOpen"):
classInfo = section.find_all_next(class_="ItemRowCenter")
parsedClasses[courseName].append((int(classInfo[0].string), int(classInfo[1].string), int(classInfo[2].string)))
print parsedClasses
print parsedClasses['FL2014' + 'A46' + '3284']
driver.quit()
好吧,你可以通过以下方法来加快速度:
由于这是
ASP.NET
生成的表单,并且由于它的安全特性,事情变得更加复杂。下面是完整的代码,不要害怕它-我已经添加了评论和开放的问题:印刷品:
当然这里还有一些需要改进的地方,比如,我已经硬编码了其他表单值——您可能应该解析可能的值并适当地设置它们。
另一个改进是将此绑定到^{} :
如您所见,当您处于更高级别并通过webdriver与浏览器交互时,您不必担心实际的请求会到达服务器以获取数据。这使得自动化变得很容易,但速度却很慢。当您深入到底层自动化时,您有更多的选项来加快速度,但是实现复杂性增长得非常快。另外,想想这种解决方案有多可靠。所以可能是坚持“黑匣子”解决方案,并与
selenium
呆在一起?我还尝试使用以下方法解决问题:
但由于不同的原因失败(可以提供相关的错误消息)。不过,这三种工具都应该有助于简化解决方案。
另请参见类似的线程:
相关问题 更多 >
编程相关推荐