如何将文本文件中的 URLS 存储在数组中以使用 selenium 浏览器访问?
How to store URLS from text file in an array to visit with selenium browser?
我试过这个:
def ad_open_file(ad_chrome):
ad_url_list = []
for line in ad_url_list:
ad_url_list.append(line)
所以我希望数组看起来像这样:
ad_url_list = ['https://www.link.org', 'https://www.link.org']
之后,我想用硒浏览器访问每个 URL,中间有 time.sleep(5)。是通过for循环完成的吗?
谁能帮我解决这个问题?
要使用 Selenium 浏览器访问每个 URL 并在访问之间休眠,您可以试试这个:
from selenium import webdriver
from time import sleep
# first get lines from the file -- assuming ad_chrome is your file path?
with open(ad_chrome) as f:
# lines is a list containing each line in the file, as a list item
lines = f.readlines()
# start the webdriver
driver=webdriver.Chrome()
# now loop through lines and visit each URL
for url in lines:
# visit the URL
driver.get(url.rstrip()) # call rstrip() to remove all trailing whitespace
# wait 5 seconds
sleep(5)
希望这可以帮助您入门。我们不需要将文件内容保存到数组中,因为我们可以遍历文件中的每一行,所以将文件行放在数组中有点多余。
我们在每一行调用 rstrip()
以删除文件中可能存在的尾随空格和换行符。
此代码假设您的文件类似于:
www.someurl.com
www.anotherurl.com
www.google.com
等..
我试过这个:
def ad_open_file(ad_chrome):
ad_url_list = []
for line in ad_url_list:
ad_url_list.append(line)
所以我希望数组看起来像这样:
ad_url_list = ['https://www.link.org', 'https://www.link.org']
之后,我想用硒浏览器访问每个 URL,中间有 time.sleep(5)。是通过for循环完成的吗?
谁能帮我解决这个问题?
要使用 Selenium 浏览器访问每个 URL 并在访问之间休眠,您可以试试这个:
from selenium import webdriver
from time import sleep
# first get lines from the file -- assuming ad_chrome is your file path?
with open(ad_chrome) as f:
# lines is a list containing each line in the file, as a list item
lines = f.readlines()
# start the webdriver
driver=webdriver.Chrome()
# now loop through lines and visit each URL
for url in lines:
# visit the URL
driver.get(url.rstrip()) # call rstrip() to remove all trailing whitespace
# wait 5 seconds
sleep(5)
希望这可以帮助您入门。我们不需要将文件内容保存到数组中,因为我们可以遍历文件中的每一行,所以将文件行放在数组中有点多余。
我们在每一行调用 rstrip()
以删除文件中可能存在的尾随空格和换行符。
此代码假设您的文件类似于:
www.someurl.com
www.anotherurl.com
www.google.com
等..