02 练习爬取网上书店

    技术2022-08-31  98

    # Author:Nimo_Ding ''' 第一个练习: 题目要求: 你需要爬取的是网上书店Books to Scrape中所有书的分类类型,并且将它们打印出来。 它的位置就在网页的左侧,如:Travel,Mystery,Historical Fiction…等。 http://books.toscrape.com/ ''' import requests from bs4 import BeautifulSoup html=requests.get('http://books.toscrape.com/') soup=BeautifulSoup(html.text,'html.parser') # for i in soup.find_all('ul',class_='nav nav-list'): # a=i.find('li').find('ul').find_all('li') # for i in a: # print(i.find('a').text.replace(' ','').replace('\n','')) # print(i.find('a').text.strip()) # 其实不用写这么多步骤,直接取text即可: for i in soup.find_all('ul',class_='nav nav-list'): # print(i.text.replace(' ','').replace('\n\n\n\n\n\n\n','\n')) print(i.text.strip()) ''' 第二个练习: 题目要求: 你需要爬取的是网上书店Books to ScrapeTravel这类书中, 所有书的书名、评分、价格三种信息,并且打印提取到的信息。 http://books.toscrape.com/catalogue/category/books/travel_2/index.html ''' import requests from bs4 import BeautifulSoup html=requests.get('http://books.toscrape.com/') soup=BeautifulSoup(html.text,'html.parser') name=[] price=[] star=[] book_name=soup.find_all('h3') for i in book_name: name.append(i.find('a')['title']) rating_price=soup.find_all(class_='product_pod') for i in rating_price: price.append(i.find(class_='price_color').text) star.append(i.find('p')['class'][1]) for i in range(len(name)): print('书名为:{},价格为:{},评星为:{}'.format(name[i],price[i],star[i]))

     

    Processed: 0.011, SQL: 9