如何生成完整目标URL列表以适配Selenium多URL遍历需求?
生成完整URL列表用于Selenium多链接处理
原代码中的循环每次仅会覆盖target_urls变量,最终只保留最后一个生成的URL。要得到包含所有完整目标URL的列表,可通过以下两种方式实现:
原代码
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep from datetime import datetime import pandas as pd import warnings urls = ['england/premier-league/brentford-brighton/23ixII53/','england/premier-league/fulham-bournemouth/pStsHxL9/'] for i in urls: target_urls = f'https://www.betexplorer.com/soccer/{i}'
解决方案1:列表推导式(简洁高效)
用列表推导式一次性生成完整URL列表,代码更紧凑:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep from datetime import datetime import pandas as pd import warnings base_url = 'https://www.betexplorer.com/soccer/' urls = ['england/premier-league/brentford-brighton/23ixII53/','england/premier-league/fulham-bournemouth/pStsHxL9/'] # 生成完整URL列表 target_urls = [base_url + path for path in urls]
解决方案2:循环追加(直观易懂)
先初始化空列表,再在循环中把每个生成的URL追加进去:
from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from time import sleep from datetime import datetime import pandas as pd import warnings urls = ['england/premier-league/brentford-brighton/23ixII53/','england/premier-league/fulham-bournemouth/pStsHxL9/'] # 初始化空列表 target_urls = [] for path in urls: full_url = f'https://www.betexplorer.com/soccer/{path}' target_urls.append(full_url)
生成的target_urls列表会包含你需要的两个完整URL,之后即可遍历该列表,用Selenium依次处理每个链接。
内容的提问来源于stack exchange,提问作者Paul Corcoran
相关产品推荐
相关产品推荐

