当前位置: 首页> 房产> 家装 > 爬虫案例(读书网)

爬虫案例(读书网)

时间:2025/7/11 1:28:03来源:https://blog.csdn.net/eqwaak0/article/details/139306808 浏览次数:0次

一.我们还是使用简单的bs4库和lxml,使用xpath:

导入下面的库:

import requests
from bs4 import BeautifulSoup
from lxml import etree

我们可以看见它的div和每个书的div框架,这样会观察会快速提高我们的简单爬取能力。

二.实例代码:

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36'}
link="https://www.dushu.com/"
r=requests.get(link,headers=headers)
r.encoding='utf-8'soup=BeautifulSoup(r.text,'lxml')
house_list=soup.find_all('div',class_="border books-center")
html=etree.HTML(r.text)# name=html.xpath('//div[@class="property-content-title"]/h3/text()')
# for house in house_list:
#     name=soup.find('div',class_="nlist").a.strong.text()
#
#     print(name)
name=html.xpath('//div[@class="bookname"]/a/text()')
# href=html.xpath('//div[@class="nlist"]/div/ul/li/a/@href')print(name)
for i in name:print(i)

运行结果如下:

 我们成功抓取了网页上书籍的名字,我们可以把它放入一个文件或者文本里面。

三.总结

我们简单的抓取书籍,先找到它需要的大div或者是ur、然后在里面找到自己需要抓取的数据,我们开始练习这样简单案例,会提高爬虫的理解。下次我会加上数据库和可视化

关键字:爬虫案例(读书网)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: