Python Selenium使用URL列表
我有一个名为all_url.txt的URL文本列表。文本文件中的每个url位于一行上。我想将此列表传递给selenium(python)以提取特定数据。我可以通过一个接一个地使用url来实现这一点,但这并不高效。我目前的代码如下:-Python Selenium使用URL列表,python,selenium,Python,Selenium,我有一个名为all_url.txt的URL文本列表。文本文件中的每个url位于一行上。我想将此列表传递给selenium(python)以提取特定数据。我可以通过一个接一个地使用url来实现这一点,但这并不高效。我目前的代码如下:- profile = FirefoxProfile('/home/test/.mozilla/firefox/mfgrtrtr.Default3') browser = webdriver.Firefox(firefox_profile=profile) browse
profile = FirefoxProfile('/home/test/.mozilla/firefox/mfgrtrtr.Default3')
browser = webdriver.Firefox(firefox_profile=profile)
browser.maximize_window()
# get website
browser.get('https://www.some-website.com/')
# get current url
print browser.current_url
# get name & get phone number
name = browser.find_element_by_class_name("name")
print name.text
phone = browser.find_element_by_class_name("phone")
print phone.text
如何将列表传递给browser.get,并从每个url提取姓名和电话。提前感谢您的帮助,我是python新手,但很享受挑战 您可能需要一个
for
循环,它可以遍历列表。您的代码应该如下所示:
profile = FirefoxProfile('/home/test/.mozilla/firefox/mfgrtrtr.Default3')
browser = webdriver.Firefox(firefox_profile=profile)
browser.maximize_window()
with open("your_file_name") as in_file:
for url in in_file:
# get website
browser.get(url.strip())
# get current url
print browser.current_url
# get name & get phone number
name = browser.find_element_by_class_name("name")
print name.text
phone = browser.find_element_by_class_name("phone")
print phone.text
URL上的
.strip
方法调用只是确保它没有前导或尾随空格-从文件中读入的行通常包括尾随的换行符。您可能需要一个for
循环,它可以在列表上迭代。您的代码应该如下所示:
profile = FirefoxProfile('/home/test/.mozilla/firefox/mfgrtrtr.Default3')
browser = webdriver.Firefox(firefox_profile=profile)
browser.maximize_window()
with open("your_file_name") as in_file:
for url in in_file:
# get website
browser.get(url.strip())
# get current url
print browser.current_url
# get name & get phone number
name = browser.find_element_by_class_name("name")
print name.text
phone = browser.find_element_by_class_name("phone")
print phone.text
URL上的.strip
方法调用仅确保它没有前导或尾随空格-从文件中读入的行通常包括尾随的换行符。打开文件:
my_file = open("all_urls.txt", "r")
对其进行迭代,并对每个url使用get
函数:
for url in my_file:
browser.get(url)
print ...
print ...
打开文件:
my_file = open("all_urls.txt", "r")
对其进行迭代,并对每个url使用get
函数:
for url in my_file:
browser.get(url)
print ...
print ...
您知道如何打开文件并使用for循环吗
将open(yourfile)作为f:for地图中的url(str.rstrip,f).
您知道如何打开文件并使用for循环吗<代码>打开(您的文件)作为f:for地图中的url(str.rstrip,f).