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

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

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

python 复制代码
# 要求:使用 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()方法。
python 复制代码
# 实验 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****的通知公告的信息。
python 复制代码
# 实验 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'))
相关推荐
Rust研习社11 分钟前
深入 Rust 引用计数智能指针:Rc 与 Arc 从入门到实战
开发语言·后端·rust
m0_6403093012 分钟前
c++如何判断两个文件路径是否物理指向同一个磁盘文件_equivalent【详解】
jvm·数据库·python
CRMEB系统商城12 分钟前
国内开源电商系统的格局与演变——一个务实的技术视角
java·大数据·开发语言·小程序·开源·php
数智工坊21 分钟前
深度拆解AnomalyAny:异常检测新工作,利用Stable Diffusion生成真实多样异常样本!
人工智能·pytorch·python·stable diffusion
xyq202425 分钟前
Eclipse 安装(Neon 版本)指南
开发语言
Shorasul25 分钟前
Django 信号中为 ImageField 指定自定义保存路径的正确实践
jvm·数据库·python
Wyz2012102431 分钟前
CSS如何实现移动端点击高亮去除_设置tap-highlight-color
jvm·数据库·python
冰暮流星32 分钟前
javascript之DOM更新操作
开发语言·javascript·ecmascript
日光明媚33 分钟前
SoulX-FlashTalk 技术报告解读:从“严格因果”到“双向流式蒸馏”,实时数字人为什么能做到 0.87s 延迟、32FPS 和长时稳定?
人工智能·python·深度学习·ai作画·aigc·音视频
粉嘟小飞妹儿37 分钟前
如何在云主机上安装Oracle 19c_公网IP绑定与安全组端口开放
jvm·数据库·python