Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python selenium:从url列表文本文件中随机选择_Python_Selenium - Fatal编程技术网

Python selenium:从url列表文本文件中随机选择

Python selenium:从url列表文本文件中随机选择,python,selenium,Python,Selenium,我有这个文本文件(links.txt),其中包含一个url列表(100个url)。我可以用下面的代码迭代url列表,代码目前可以正常工作。但是,我只想随机迭代列表中的36个URL for i in range(20): with open("links.txt") as in_file: for url in in_file: driver.get(url.strip()) try:

我有这个文本文件(links.txt),其中包含一个url列表(100个url)。我可以用下面的代码迭代url列表,代码目前可以正常工作。但是,我只想随机迭代列表中的36个URL

for i in range(20):
    with open("links.txt") as in_file:
         for url in in_file:
              driver.get(url.strip())
            try:
                items1 = driver.find_elements_by_xpath("//div[@class='firms']//div")
                sampled = random.sample(list(items1), 1)
                for items in sampled:
                    items.click()
            except:
                continue
driver.find_element_by_xpath("//input[@value='houses']").click()

这是随机的任务。示例,您还需要读取所有行,而不是逐行迭代,即替换

for url in in_file:
使用

请注意,
random_url
中的链接顺序可能与
url
中的链接顺序不同

urls = in_file.readlines()
random_urls = random.sample(urls, 36)
for url in random_urls: