簡體   English   中英

從Python的CGI腳本運行硒的webdriver

[英]Running Selenium webdriver from a Python CGI script

我創建了一個使用Selenium webdriver抓取網站的python腳本。 現在,我正在嘗試使用CGI從網絡運行此腳本。 因此,為確保我的CGI服務器正常工作,我嘗試了以下操作:

import cgi
print 'Content-Type: text/html'
print
list_brand = ['VOLVO','FIAT', 'BMW']
print '<h1>TESTING CGI</h1>'
print '<form>'
print '<select>'
for i in range(3):
      print '<option value="' + list_brand[i] + '">'+ list_brand[i] +'</option>'
print '</select>'
print '</form>'

而且效果很好。 現在,當我使用此腳本將Selenium與CGI結合使用時:

import cgitb
import cgi
from selenium import webdriver

print 'Content-Type: text/html'
print
cgitb.enable(display=0, logdir="C:/path/to/log/directory")
path_to_pjs = 'C:path/to/phantomjs-2.1.1-windows/bin/phantomjs.exe'
browser = webdriver.PhantomJS(executable_path = path_to_pjs)
#Reaching to URL
url = 'http://www.website.fr/cl/2/products'
browser.get(url)
div_set = browser.find_elements_by_class_name('productname')
print '<form>'
print '<select>'
for div in div_set:
      print '<option value="' + div.find_element_vy_tag_name('h3').text + '">'+ div.find_element_vy_tag_name('h3').text +'</option>'
print '</select>'
print '</form>'

該頁面不斷加載,但沒有響應。 知道這是否可能(我是說從cgi腳本運行selenium)還是為什么我的服務器沒有響應?

好吧,我發現我的問題的解決方案! 一:我沒注意,我寫vy ,而不是by我的功能: div.find_element_by_tag_name 第二件事是使用Apache服務器。 由於某些原因,使用CGIHTTPServer的lite python服務器無法正常工作。 所以我用XAMPP修改httpd.conf文件的最后一件事是將路徑#!/Python27/python的腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM