主题资源网站建设模块五作业,360提交网站入口,国内什么网站用asp.net,app怎么制作视频实验 1#xff1a;爬取网页中的数据。 要求#xff1a;使用 urllib 库和 requests 库分别爬取 http://www.sohu.com 首页的前 360 个字节的数据。 # 要求#xff1a;使用 urllib 库和 requests 库分别爬取 http://www.sohu.com 首页的前 360 个字节的数据。
import urllib.r… 实验 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))