![](/img/trans.png)
[英]How to use CSS selectors to retrieve specific links lying in some class using BeautifulSoup?
[英]How to use CSS selectors to retrieve specific links using BeautifulSoup?
我使用 python 來抓取以下頁面: alfabeta.surge.sh ,我想在 (#home1 > div:nth-child(10) > table:nth-child(29) > tbody > tr:nth 中獲取鏈接-child(1) > td:nth-child(3) > a)
實際上我正在這樣做:
import bs4, requests
res = requests.get('https://alfabeta.surge.sh/')
soup = bs4.BeautifulSoup(res.text, 'html.parser')
soup.find_all('a')[23].attrs.get('href')
但是如果更改的 position 我無法下載內容
您需要對最有可能保持不變的內容做出一些假設,然后隨着時間的推移進行審查。 例如,我可能假設您希望第 3 列td
的子項a
標簽href
,該table
是div
后面的第一個包含字符串Catálogo Actualizaciones
的表。 一種 css 模式如下:
import requests
from bs4 import BeautifulSoup as bs
r = requests.get('https://alfabeta.surge.sh/')
soup = bs(r.text, 'lxml')
print(soup.select_one('div:-soup-contains("Catálogo Actualizaciones") ~ table td:nth-child(3) > a')['href'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.