【发布时间】:2014-10-10 23:27:45
【问题描述】:
我在 Python 中使用 Selenium webdriver 在搜索字段中输入一些文本并查找它。我现在想解析该页面/在其上使用 BeautifulSoup 之类的东西。但是我对如何调用结果页面感到困惑。
到目前为止我的代码:
textinput = open("1.txt", "r").read()
url = "http://www.example.com"
driver = webdriver.Chrome(executable_path='path/chromedriver.exe')
driver.get(url)
sbox = driver.find_element_by_name("a")
sbox.send_keys(textinput)
submit = driver.find_element_by_xpath('//*[@id="maincontent"]/form/input[5]')
submit.click()
【问题讨论】:
-
当您输入文本时,然后用鼠标单击“go”、“search”等按钮或打开结果页面的按钮。然后您可以使用该页面的源代码来提取数据。请分享您的代码
-
@Vipul 请找到添加的代码。我能够得到结果。但我需要解析它。我无法手动复制结果页面的 URL,因为我会这样做数百次。我想自动化它。
-
使用
driver.source可以获得源码 -
您也不需要获取源代码或使用 beautifulsoup,使用 selenium 驱动程序对象本身进行解析就足够了,并且通过使用
driver.find_element_by_<id,xpath...>或 'driver.find_elements_by_ 中的 class、id、tag、xpath 非常干净'
标签: python html selenium selenium-webdriver beautifulsoup