如何将循环应用于工作的Python Selenium脚本?
我正试图找出如何将for循环应用于此脚本,但遇到了很多麻烦。我想遍历以csv格式存储的子域列表(即:一列20个子域),并打印每个子域的html。它们都有相同的源域。谢谢如何将循环应用于工作的Python Selenium脚本?,python,list,csv,selenium,loops,Python,List,Csv,Selenium,Loops,我正试图找出如何将for循环应用于此脚本,但遇到了很多麻烦。我想遍历以csv格式存储的子域列表(即:一列20个子域),并打印每个子域的html。它们都有相同的源域。谢谢 #Python 2.6 from selenium import selenium import unittest, time, re, csv, logging class Untitled(unittest.TestCase): def setUp(self): self.verificationE
#Python 2.6
from selenium import selenium
import unittest, time, re, csv, logging
class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*firefox", "http://www.SourceDomain.com")
self.selenium.start()
def test_untitled(self):
sel = self.selenium
sel.open("/dns/www.subdomains.com.html")
sel.wait_for_page_to_load("30000")
html = sel.get_html_source()
print html
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
顺便说一句,注意没有必要将这个脚本包装在unittest测试用例中。更妙的是,您不需要硒来完成如此简单的任务(至少乍一看是这样)
试试这个:
import urllib2, csv
def fetchsource(url):
page = urllib2.urlopen(url)
source = page.read()
return source
fooReader = csv.reader(open('your_file.csv'))
for url in fooReader:
print fetchsource(url)
顺便说一句,注意没有必要将这个脚本包装在unittest测试用例中。更妙的是,您不需要硒来完成如此简单的任务(至少乍一看是这样)
试试这个:
import urllib2, csv
def fetchsource(url):
page = urllib2.urlopen(url)
source = page.read()
return source
fooReader = csv.reader(open('your_file.csv'))
for url in fooReader:
print fetchsource(url)
谢谢-我现在正试着测试你的第一个答案。我无法让urllib2工作,因为这些页面使用了大量javaScript。为此,Alex Martelli建议我使用Selenium。我一直遇到语法错误,因为我忘记了(open('your_file.csv'))上的第二个右括号:-P它可以工作!非常感谢。啊,这是你在这类任务中使用硒的一个小原因。很高兴看到它有帮助。谢谢-我现在正试着测试你的第一个答案。我无法让urllib2工作,因为这些页面使用了大量javaScript。为此,Alex Martelli建议我使用Selenium。我一直遇到语法错误,因为我忘记了(open('your_file.csv'))上的第二个右括号:-P它可以工作!非常感谢。啊,这是你在这类任务中使用硒的一个小原因。很高兴看到它有所帮助。