你需要爬取的是网上书店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>

 

 
 

相关文章:

  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-18
  • 2021-05-03
  • 2021-10-22
猜你喜欢
  • 2022-03-09
  • 2022-12-23
  • 2021-05-27
  • 2021-08-03
  • 2021-10-09
  • 2021-12-12
  • 2022-01-28
相关资源
相似解决方案