Python爬虫精进-第2关博客爬虫
2021/7/12 20:06:29
本文主要是介绍Python爬虫精进-第2关博客爬虫,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Python爬虫精进-第2关博客爬虫
练习介绍
你已经学习了用bs库解析数据和提取数据的方法,只要数据在HTML源代码中,你都可以拿到了。
要求:
爬取博客【人人都是蜘蛛侠】中,《未来已来(四)——Python学习进阶图谱》文章的默认评论(不需要点击下一页),并且打印。
文章链接:https://wordpress-edu-3autumn.localprod.oc.forchange.cn/all-about-the-future_04/
目的:
- 练习获取网页源代码,然后使用
BeautifulSoup
解析提取数据。 - 实操爬虫的前三个步骤。
python参考解答
''' Author: Gu Jiakai Date: 2021-07-12 18:59:24 LastEditTime: 2021-07-12 19:08:20 LastEditors: Gu Jiakai Description: FilePath: \第2关-爬虫初体验\习题再练-博客爬虫.py ''' #调用requests库。 import requests #调用BeautifulSoup库。 from bs4 import BeautifulSoup #文章链接。 url1='https://wordpress-edu-3autumn.localprod.oc.forchange.cn/all-about-the-future_04/' #返回一个response对象,赋值给res。 res=requests.get(url1) # bs对象=BeautifulSoup(要解析的文本,'解析器') # 把网页解析为BeautifulSoup对象。 soup=BeautifulSoup(res.text,'html.parser') # 通过定位标签和属性提取我们想要的数据。 lst=soup.find_all('div',class_='comment-content') for ele in lst: print(ele.text.strip()) # Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。 ''' result; 小爷的第三次评论来了 好,好,好,非常好 小爷>纯属测试 am I the first? 人人都是蜘蛛侠 第10个蜘蛛侠 第9个蜘蛛侠 第8个蜘蛛侠 第7个蜘蛛侠 第6个蜘蛛侠 第5个蜘蛛侠 第4个蜘蛛侠 第3个蜘蛛侠 第2个蜘蛛侠 第1个蜘蛛侠 '''
这篇关于Python爬虫精进-第2关博客爬虫的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python