02、书店寻宝(一)

    你需要爬取的是网上书店Books to Scrape中所有书的分类类型,并且将它们打印出来。
 
    它的位置就在网页的左侧,如:Travel,Mystery,Historical Fiction…等。
 
 
 1 #2、书店寻宝(一)
 2 #    你需要爬取的是网上书店Books to Scrape中所有书的分类类型,并且将它们打印出来。
 3 #    它的位置就在网页的左侧,如:Travel,Mystery,Historical Fiction…等。
 4 #    网页URL:http://books.toscrape.com/
 5 
 6 import requests
 7 from bs4 import BeautifulSoup
 8 res = requests.get('http://books.toscrape.com/')
 9 html = res.text
10 soup = BeautifulSoup(html,'html.parser')
11 items = soup.find('ul',class_='nav nav-list').find('li').find_all('li')
12 
13 for item in items:
14     print(item.find('a').text.strip())
15     print(item.find('a').text.replace('\n','').replace(' ',''))
16 
17 '''
18 执行结果如下:
19 Travel
20 Mystery
21 HistoricalFiction
22 SequentialArt
23 Classics
24 Philosophy
25 '''

 

items中每个Tag的内容如下
 
1 <li>
2 <a href="catalogue/category/books/crime_51/index.html">
3 
4     Crime
5 
6 </a>
7 </li>

 

 
 
posted @ 2019-04-11 20:34  三角形  阅读(954)  评论(0编辑  收藏  举报