page = requests.get(base_url)
soup = BeautifulSoup(page.text, "html.parser")
books = soup.find_all('li', attrs={'class':'col-xs-6 col-sm-4 col-md-3 col-lg-3'})
star = ['One', 'Two', 'Three', 'Four', 'Five']
res, book_no = [], 1
# Iterate books classand check for the given tags
for book in books:
title = book.find('img')['alt']
link = base_url[:37] + book.find('a')['href']
for index in range(5):
find_stars = book.find('p', attrs={'class': 'star-rating ' + star[index]})
if find_stars is not None:
stars = star[index] + " out of 5"
break
price = book.find('p', attrs={'class': 'price_color'}).text
instock = book.find('p', attrs={'class':'instock availability'}).text.strip()
data = {'book no': str(book_no), 'title': title,'rating': stars, 'price': price, 'link': link,'stock': instock}
# Append the dictionary to the list
res.append(data)
book_no += 1
return res
# Main Function
if __name__ == "__main__":
# Enter the url of website
base_url = "https://books.toscrape.com/catalogue/page-1.html"
res = json_from_html_using_bs4(base_url)
# it to books.json file.
with open('books.json', 'w', encoding='latin-1') as f:
json.dump(res, f, indent=8, ensure_ascii=False)
print("Created Json File")
一个关于Python、WordPress主题、
WordPress外贸建站
与网络营销的个人博客;
分享一些Wordpress建站技巧、Google Ads优化分析和其他
WordPress资源
,使我们这些WordPress初学者可以改善站点,共同进步;
小白一枚……