我正在嘗試從此
uj5u.com熱心網友回復:
您可以使用
for img in soup.find_all(lambda x: x.name == 'img' and 'automatica.gif' in x['src']):
print(img.next_sibling.next_sibling['href'])
筆記:
soup.find_all(lambda x: x.name == 'img' and 'automatica.gif' in x['src'])- 獲取img包含automatica.gif在src屬性中的所有節點img.next_sibling.next_sibling['href']- 獲取href每個找到的img標簽的第二個兄弟的值。
uj5u.com熱心網友回復:
根據偏好,我會使用 css 選擇器來提高速度,并簡單地使用 src 過濾 img 包含automatica. 然后移動到相鄰的a標簽,使用相鄰的同級組合符 ( ),并提取href.
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('http://meteo.navarra.es/estaciones/descargardatos.cfm')
soup = bs(r.content, 'lxml')
automaticas = ['http://meteo.navarra.es/estaciones/' i['href'] for i in soup.select('img[src*=automatica] a')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/368367.html
