此代码应该可以工作!
import requests
from bs4 import BeautifulSoup
URL = 'http://books.toscrape.com/catalogue/page-1.html'
page = requests.get(URL)
soup = BeautifulSoup(page.content, 'html.parser')
list_of_books = soup.select(
'#default > div > div > div > div > section > div:nth-child(2) > ol > li'
)
for book in list_of_books:
price = book.find('p', {'class': 'price_color'})
print(price.text.strip())
我刚用了chorme选择器
this is a screenshot of it
您正在使用
find
和
find_all
在错误的地方。