我是新手,正在尝试一个示例,使用selenium和Python进行网络抓取。示例链接在这里:http://www.marinamele.com/selenium-tutorial-web-scraping-with-selenium-and-python。
火狐浏览器打开后立即关闭并显示“Firefox意外退出”。
我已经阅读了Selenium无头浏览器webdriver [Errno 104]连接被对等方重置和为什么我在Python中收到此错误?(httplib),但没有帮助。
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
def init_driver():
driver = webdriver.Firefox()
driver.wait = WebDriverWait(driver, 5)
return driver
def lookup(driver, query):
driver.get("http://www.google.com")
try:
box = driver.wait.until(EC.presence_of_element_located(
(By.NAME, "q")))
button = driver.wait.until(EC.element_to_be_clickable(
(By.NAME, "btnK")))
box.send_keys(query)
button.click()
except TimeoutException:
print("Box or Button not found in google.com")
if __name__ == "__main__":
driver = init_driver()
lookup(driver, "Selenium")
time.sleep(5)
driver.quit()
我已经使用安装了selenium
pip install selenium
但它不起作用。
出现的错误是
socket.error: [Errno 54] Connection reset by peer
有时候
raise BadStatusLine(line)
httplib.BadStatusLine: ''
火狐浏览器打开后立即关闭并显示“Firefox意外退出”。
我已经阅读了Selenium无头浏览器webdriver [Errno 104]连接被对等方重置和为什么我在Python中收到此错误?(httplib),但没有帮助。