请选择 进入手机版 | 继续访问电脑版

 找回密码
 立即注册
搜索
热搜: Excel discuz
查看: 3262|回复: 3

获取一个简单的页面

[复制链接]

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750844
发表于 2020-8-6 17:35:50 | 显示全部楼层 |阅读模式
B站上看视频,抄的一个代码,获取首页页面数据的爬虫
  1. #爬取本站首页页面数据
  2. #导入requests模块
  3. import requests
  4. #指定URL
  5. url = 'http://www.zhaoqianzhou.com'
  6. #发起请求,get方法会返回一个响应对象
  7. response = requests.get(url=url)
  8. #获取响应数据.text返回的是字符串形式的响应数据
  9. page = response.text
  10. print(page)
  11. #存储
  12. with open('./sodgou.html','w',encoding='utf-8') as fp:
  13.     fp.write(page)
  14. print('爬虫is over')
复制代码




回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750844
 楼主| 发表于 2020-8-6 17:36:41 | 显示全部楼层
生成的HTML文件,存储在同一目录下
回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750844
 楼主| 发表于 2020-8-6 19:03:44 | 显示全部楼层
  1. import requests
  2. url = 'http://www.zhaoqianzhou.com'
  3. respsonse = requests.get(url=url)
  4. page_text = respsonse.text
  5. with open('./qianzhou.html','w',encoding='utf-8') as fp:
  6.     fp.write(page_text)
  7. print(page_text)
  8. print('over')
复制代码
回复

使用道具 举报

482

主题

7万

元宝

75万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
750844
 楼主| 发表于 2021-5-20 15:53:22 | 显示全部楼层
  1. import requests
  2. url = 'https://club.coovm.com/forum-53-1.html'
  3. spon = requests.get(url=url)
  4. print(spon.text)                    #获得HTML网页数据
  5. print(spon.content)                 #获得返回的数据(二进制)
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|五花八门论坛 ( 豫ICP备15031300号-3 )

GMT+8, 2024-3-29 04:53 , Processed in 0.091111 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表