你需要爬取的是网上书店Books to Scrape中所有书的分类类型,并且将它们打印出来。       它的位置就在网页的左侧,如:Travel,Mystery,Historical Fiction…等。       网页URL: http://books.toscrape.com/  
 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>

 

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。    
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄