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'))
相关推荐
雾月553 小时前
LeetCode 941 有效的山脉数组
java·开发语言·数据结构·算法·leetcode·职场和发展
晨曦5432104 小时前
函数和模式化——python
开发语言·python
leluckys4 小时前
swift-08-属性、汇编分析inout本质
开发语言·汇编·swift
Verdure陌矣5 小时前
游戏开发中 C#、Python 和 C++ 的比较
c++·python·游戏·c#
嘤国大力士6 小时前
C++11&QT复习 (十六)
java·开发语言·c++
丰锋ff6 小时前
借助 AI 工具使用 Python 实现北京市店铺分布地理信息可视化教程
人工智能·python·信息可视化
念九_ysl6 小时前
Java中的列表(List):操作与实现详解
java·开发语言·list
阿昆的科研日常6 小时前
Matlab进阶绘图第74期-带填充纹理的单组柱状图
开发语言·matlab·可视化·论文插图