.select()和.find_all()的接口不同。 select() 接受 CSS 选择器 (list of all CSS selectors that BeautifulSoup 4.7.1+ supports),find_all() 不接受 (list of bs4 filters)。
要选择所有带有"quote" 类的标签,您可以使用soup.find_all(class_="quote"):
import requests
from bs4 import BeautifulSoup
url = "http://quotes.toscrape.com"
url_next = "/page/1"
ori_url = requests.get(f"{url}{url_next}").text
every_thang = []
soup = BeautifulSoup(ori_url, "html.parser")
all_the_quotes2 = soup.find_all(class_="quote")
every_thang = []
for q in all_the_quotes2:
every_thang.append({
"text": q.find(class_="text").get_text(),
"author": q.find(class_="author").get_text(),
"linky": q.find("a")["href"]
})
from pprint import pprint
pprint(every_thang)
打印:
[{'author': 'Albert Einstein',
'linky': '/author/Albert-Einstein',
'text': '“The world as we have created it is a process of our thinking. It '
'cannot be changed without changing our thinking.”'},
{'author': 'J.K. Rowling',
'linky': '/author/J-K-Rowling',
'text': '“It is our choices, Harry, that show what we truly are, far more '
'than our abilities.”'},
{'author': 'Albert Einstein',
'linky': '/author/Albert-Einstein',
'text': '“There are only two ways to live your life. One is as though '
'nothing is a miracle. The other is as though everything is a '
'miracle.”'},
{'author': 'Jane Austen',
'linky': '/author/Jane-Austen',
'text': '“The person, be it gentleman or lady, who has not pleasure in a '
'good novel, must be intolerably stupid.”'},
{'author': 'Marilyn Monroe',
'linky': '/author/Marilyn-Monroe',
'text': "“Imperfection is beauty, madness is genius and it's better to be "
'absolutely ridiculous than absolutely boring.”'},
{'author': 'Albert Einstein',
'linky': '/author/Albert-Einstein',
'text': '“Try not to become a man of success. Rather become a man of '
'value.”'},
{'author': 'André Gide',
'linky': '/author/Andre-Gide',
'text': '“It is better to be hated for what you are than to be loved for '
'what you are not.”'},
{'author': 'Thomas A. Edison',
'linky': '/author/Thomas-A-Edison',
'text': "“I have not failed. I've just found 10,000 ways that won't work.”"},
{'author': 'Eleanor Roosevelt',
'linky': '/author/Eleanor-Roosevelt',
'text': '“A woman is like a tea bag; you never know how strong it is until '
"it's in hot water.”"},
{'author': 'Steve Martin',
'linky': '/author/Steve-Martin',
'text': '“A day without sunshine is like, you know, night.”'}]