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'))
相关推荐
WXX_s3 分钟前
【OpenCV篇】OpenCV——03day.图像预处理(2)
人工智能·python·opencv·学习·计算机视觉
橙小花8 分钟前
C语言:指针、变量指针与指针变量、数组指针与指针数组
c语言·开发语言
Cyanto27 分钟前
MyBatis-Plus高效开发实战
java·开发语言·数据库
艾莉丝努力练剑43 分钟前
【LeetCode&数据结构】二叉树的应用(二)——二叉树的前序遍历问题、二叉树的中序遍历问题、二叉树的后序遍历问题详解
c语言·开发语言·数据结构·学习·算法·leetcode·链表
Jackilina_Stone1 小时前
【论文|复现】YOLOFuse:面向多模态目标检测的双流融合框架
人工智能·python·目标检测·计算机视觉·融合
wjs20241 小时前
XML 语法详解
开发语言
双叶8362 小时前
(Python)文件储存的认识,文件路径(文件储存基础教程)(Windows系统文件路径)(基础教程)
开发语言·windows·python
喜欢吃燃面2 小时前
C++:list(1)list的使用
开发语言·c++·学习
枫昕柚2 小时前
python
开发语言·python
木头左2 小时前
自动驾驶领域中的Python机器学习
python·机器学习·自动驾驶