我正在使用python和selenium-Phantomjs来执行一些url。但当它转到driver.get(url)
时,它就会卡在那里。当我使用铬而不是它时,它的工作非常好。
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver
import time
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/53 (KHTML, like Gecko) Chrome/56.0.2924.87")
def Foo(url):
try:
driver = webdriver.PhantomJS(desired_capabilities=dcap,service_args=['--ignore-ssl-errors=true'])
# chromedriver = "/Documents/chromedriver"
# os.environ["webdriver.chrome.driver"] = chromedriver
# driver = webdriver.Chrome(executable_path=r"Path To Chrome Driver")
driver.get(url)
time.sleep(8)
status = "successful"
except (Exception) as e:
print(e)
status = "unsuccessful"
driver.close()
return status
url ="https://www.zomato.com/clients/reviews_new.php?entity_type=restaurant&entity_id=53892&start_date=2012-01-01&end_date=2017-05-05"
status = Foo(url)
print(status)
任何建议都会有所帮助。
答案 0 :(得分:0)
好的,我自己找到了替代方案。我只使用无头镀铬而不是幻影。
from selenium import webdriver
from pyvirtualdisplay import Display
import time,os
display = Display(visible=0, size=(800, 600))
display.start()
def Foo(url):
try:
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(url)
time.sleep(8)
status = "successful"
except (Exception) as e:
print(e)
status = "unsuccessful"
#driver.close()
return status
url ="https://www.zomato.com/clients/reviews_new.php?entity_type=restaurant&entity_id=53892&start_date=2012-01-01&end_date=2017-05-05"
status = Foo(url)
print(status)
但首先你必须安装xvfb
无头地运行chrome。您可以使用sudo apt-get install xvfb
进行安装。您还需要安装pyvirtualdisplay包。这可以通过pip3 install pyvirtualdisplay
完成。