关于python:尝试根据与类列表对象的链接选择元素 – beautifulsoup

Trying to select element based on link with class list object - beautifulsoup

我用的是漂亮的汤4.4和python 3.6.6。我已经提取了所有链接,但是我无法打印出包含

'class': ['_self']

这是从链接列表中获取的完整链接。

1
{'href': 'https://www.racingnsw.com.au/news/latest-racing-news/highway-sixtysix-on-right-route/', 'class': ['_self'], 'target': '_self'}

我无法得到正确的语法,尽管它看起来像属性上的BS4文档。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests as req
import json
from bs4 import BeautifulSoup

url = req.get(
    'https://www.racingnsw.com.au/media-news-premierships/latest-news/')

data = url.content

soup = BeautifulSoup(data,"html.parser")

links = soup.find_all('a')

for item in links:
    print(item['class']='self')

BeautifulSoup支持CSS选择器,它允许您根据特定属性的内容选择元素。这包括用于contains的选择器*=

1
2
3
4
5
6
7
8
9
10
11
12
import requests as req
from bs4 import BeautifulSoup

url = req.get(
    'https://www.racingnsw.com.au/media-news-premierships/latest-news/')

data = url.content

soup = BeautifulSoup(data,"html.parser")

for items in soup.select('a[class*="_self"]'):
    print(items)