当前位置: 首页 > news >正文

Python实验项目9 :网络爬虫与自动化

实验 1:爬取网页中的数据。

要求:使用 urllib 库和 requests 库分别爬取 http://www.sohu.com 首页的前 360 个字节的数据。

# 要求:使用 urllib 库和 requests 库分别爬取 http://www.sohu.com 首页的前 360 个字节的数据。
import urllib.request
import requests
# 使用 urllib 库爬取 http://www.sohu.com 首页的前 360 个字节的数据。
url = 'http://www.sohu.com'
req = urllib.request.Request(url)
res = urllib.request.urlopen(req)
data = res.read(360)
print(data)# 使用 requests 库爬取 http://www.sohu.com 首页的前 360 个字节的数据。
#url = 'http://www.sohu.com'
#res = requests.get(url)
#data = res.content[:360]
#print(data)

实验 2:测试 BeautifulSoup 对象的方法。

要求:

1)创建 BeautifulSoup 对象。
2)测试搜索文档树的 find_all()方法和 find()方法。
# 实验 2:测试 BeautifulSoup 对象的方法。
# 要求:
# 1)创建 BeautifulSoup 对象。
# 2)测试搜索文档树的 find_all()方法和 find()方法。
from bs4 import BeautifulSoup
import requests
# 过http请求加载网页
response = requests.get("http://www.sohu.com")
# 创建BeautifulSoup对象
soup = BeautifulSoup(response.text, "html.parser")
# 搜索文档树的find_all()方法
print(soup.find_all("a"))
# 搜索文档树的find()方法
print(soup.find("a"))

 

 实验 3:爬取并分析网页页面数据。

 (1)使用requests库爬取https://www.hnnu.edu.cn/main.htm首页内容。
(2)编写程序获取https://www.hnnu.edu.cn/119/list.htm的通知公告的信息。  
# 实验 3:爬取并分析网页页面数据。
# (1)使用requests库爬取https://www.hnnu.edu.cn/main.htm首页内容。
# (2)编写程序获取https://www.hnnu.edu.cn/119/list.htm的通知公告的信息。
import requests
from bs4 import BeautifulSoup
url = 'https://www.hnnu.edu.cn/main.htm'
res = requests.get(url)
soup = BeautifulSoup(res.text,'html.parser')
print(soup.find_all('a'))
print(soup.find('a'))for i in range(1,23,1):url = 'https://www.hnnu.edu.cn/119/list.htm{}.htm'.format(i)res = requests.get(url)soup = BeautifulSoup(res.text,'html.parser')print("-------------------------------------------------------")print(soup)#print(soup.find('a'))

http://www.lryc.cn/news/261762.html

相关文章:

  • 实验三:指令调度和延迟分支
  • 【Oracle】PL/SQL语法、存储过程,触发器
  • 2020年第九届数学建模国际赛小美赛C题亚马逊野火解题全过程文档及程序
  • 保姆级 Keras 实现 YOLO v3 三
  • HPM6750系列--第十篇 时钟系统
  • 【简单总结】中断类型号 中断向量 中断入口地址
  • 【Python百宝箱】从传感器到云端:深度解析Python在物联网中的多面应用
  • weston 1: 编译与运行傻瓜教程(补充)
  • 微服务保护--线程隔离(舱壁模式)
  • 集群监控Zabbix和Prometheus
  • K8S(七)—污点、容忍
  • 新视野大学英语1 词组 12.17
  • springboot实战项目之使用AOP技术实现各种角色的鉴权功能
  • 华为配置基本QinQ示例
  • 【漏洞复现】系列集合
  • TCP报文头(首部)详解
  • 第4章-第1节-初识Java的数组
  • 大数据技术10:Flink从入门到精通
  • IDEA中工具条中的debug按钮不能用了显示灰色
  • 【MySQL内置函数】
  • C++相关闲碎记录(14)
  • 18、vue3(十八):菜单权限,按钮权限,打包,发布nginx
  • 04 在Vue3中使用setup语法糖
  • vite+ts——user.ts——ts接口定义+axios请求的写法
  • 环境搭建及源码运行_java环境搭建_mysql安装
  • Android camera的metadata
  • ElasticSearch面试题
  • C++ 数据结构知识点合集-C/C++ 数组允许定义可存储相同类型数据项的变量-供大家学习研究参考
  • 【机器学习】5分钟掌握机器学习算法线上部署方法
  • Vue3-21-组件-子组件给父组件发送事件