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'))

相关推荐

  1. python实现网络爬虫

    2023-12-17 18:58:01       32 阅读
  2. Python网络爬虫项目开发实战:如何处理动态内容

    2023-12-17 18:58:01       41 阅读
  3. Python网络爬虫项目开发实战:怎么解决数据抓取

    2023-12-17 18:58:01       34 阅读
  4. Python网络爬虫项目开发实战:怎么处理下载缓存

    2023-12-17 18:58:01       36 阅读
  5. Python网络爬虫项目开发实战:如何处理并发下载

    2023-12-17 18:58:01       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-17 18:58:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-17 18:58:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-17 18:58:01       82 阅读
  4. Python语言-面向对象

    2023-12-17 18:58:01       91 阅读

热门阅读

  1. vscode

    vscode

    2023-12-17 18:58:01      53 阅读
  2. day 18二叉树(五)

    2023-12-17 18:58:01       56 阅读
  3. 《农业》特刊约稿

    2023-12-17 18:58:01       57 阅读
  4. 帆软BI目录

    2023-12-17 18:58:01       70 阅读
  5. 字节面试题(懂车帝)后端开发

    2023-12-17 18:58:01       68 阅读
  6. Python学习之复习MySQL-Day2(DML)

    2023-12-17 18:58:01       78 阅读
  7. vue使用自定义指令使用滚动加载

    2023-12-17 18:58:01       67 阅读
  8. Android手机使用Termux终端模拟器

    2023-12-17 18:58:01       86 阅读
  9. 如何设定一个N层CNN的Layer,CNN初始化

    2023-12-17 18:58:01       58 阅读
  10. Linux与常用的Linux命令

    2023-12-17 18:58:01       62 阅读