使用 Python 从多个 URL 中提取文件/(kml)

Using Python to pull files/(kml's) from multiple URLs

我是 python 的初学者,我正在尝试自动化一个涉及到网站 :http://www.wildcad.net/WildCADWeb.asp and clicking every single dispatch center, for example: http://www.wildcad.net/WCAZ-ADC.htm 的过程,从那里加载 kml。我注意到每个页面都遵循类似的格式,所以我想我可以使用 select all 函数。我把我的代码写成...

from bs4 import BeautifulSoup
import requests

urls = ('http://www.wildcad.net/WCAZ-ADC.htm', 'http://www.wildcad.net/WCALAIC.htm',
       'http://www.wildcad.net/WCAR-AOC.htm','http://www.wildcad.net/WCAZ-ADC.htm'
       'http://www.wildcad.net/WCAZ-FDC.htm', 'http://www.wildcad.net/WCAZ-PDC.htm'
       'http://www.wildcad.net/WCAZ-PHC.htm', 'http://www.wildcad.net/WCAZ-SDC.htm'
       'http://www.wildcad.net/WCAZ-TDC.htm', 'http://www.wildcad.net/WCAZ-WDC.htm')


 result = requests.get(urls)
    doc = BeautifulSoup(result.text, 'html.parser')
    print(doc.prettify())
    for i in enumerate(soup.findAll('a')):
        _KML = urls + link.get('href')
        if _KML.endswith('.kml'):
            urls.append(_KML)

    open(_KML)

但是它似乎并没有提取文件,而且我一直在“65”行收到错误消息。非常感谢任何有关如何解决此问题的指导或示例!

工作代码。请 运行 代码。

import time
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup

url = 'http://www.wildcad.net/WildCADWeb.asp'

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
time.sleep(2)
driver.get(url)
time.sleep(5)

soup = BeautifulSoup(driver.page_source, "html.parser")

for link in soup.select('table[align="center"] tbody tr td a')[1:]:
    url=link.get('href')
    #print(url)
    if url.endswith('.kml'):
        kml_url = url
        print(kml_url)

   

输出:

http://www.wildcad.net/WAearth.kml