首页 > 其他 > 详细

02、书店寻宝(一)

时间:2019-04-11 21:14:23      阅读:225      评论:0      收藏:0      [点我收藏+]
    你需要爬取的是网上书店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>

 

 
 

02、书店寻宝(一)

原文:https://www.cnblogs.com/www1707/p/10692307.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!