爬虫,初学者指南

news/2024/7/19 11:46:14 标签: 爬虫

第一篇:入门测速request模块的基本使用以www.douban.com为例

import requests
url = "http://www.douban.com"
heards = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}
r = requests.get(url=url)
print(r)
print(r.headers)

<Response [418]>
{'Connection': 'close', 'Date': 'Sat, 23 Sep 2023 14:46:44 GMT', 'Server': 'dae', 'Content-Length': '0'}

request模块用于测速发送数据的连通性,通过回复可以看出418,Connection:close表示未获取到服务器的返回值,需要添加heards信息,此服务器拒绝非浏览器发送的请求。

import requests
url = "http://www.douban.com"
heards = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
}
r = requests.get(url=url, headers=heards)
print(r)
print(r.headers)

<Response [200]>
{'Date': 'Sat, 23 Sep 2023 14:49:28 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Transfer-Encoding': 'chunked', 'Connection': 'keep-alive', 'Keep-Alive': 'timeout=30', 'X-Xss-Protection': '1; mode=block', 'X-Douban-Mobileapp': '0', 'Expires': 'Sun, 1 Jan 2006 01:00:00 GMT', 'Pragma': 'no-cache', 'Cache-Control': 'must-revalidate, no-cache, private', 'Set-Cookie': 'ck="deleted"; max-age=0; domain=.douban.com; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/, dbcl2="deleted"; max-age=0; domain=.douban.com; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/, ll="108099"; path=/; domain=.douban.com; expires=Sun, 22-Sep-2024 14:49:28 GMT, bid=ArFHHYIPdMc; Expires=Sun, 22-Sep-24 14:49:28 GMT; Domain=.douban.com; Path=/', 'X-DAE-App': 'sns', 'X-DAE-Instance': 'home', 'X-DAE-Mountpoint': 'True', 'X-DOUBAN-NEWBID': 'ArFHHYIPdMc', 'Server': 'dae', 'X-Frame-Options': 'SAMEORIGIN', 'Strict-Transport-Security': 'max-age=15552000;', 'Content-Encoding': 'gzip'}

上图可以看出添加了头信息headers之后成功获取了返回值,一般情况下使用request模块发送请求爬虫时,默认情况下都需要添加头信息


http://www.niftyadmin.cn/n/5045140.html

相关文章

物联网如何助力乡村数字经济发展

在当今移动互联网时代&#xff0c;信息化数字化已经渗透了我们生活的方方面面&#xff0c;数字物联网转型也成为各个产业的重要动力。对于广大乡村来说&#xff0c;得益于网络基础设施的全面建成覆盖&#xff0c;乡村各产业也迎来数字化新业态转型&#xff0c;创新诞生了基于物…

【国科大卜算】Truck History 最小生成树Prim

Truck History 文章目录 Truck Historyproblem descriptionInputOutputSample个人理解 problem description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company…

力扣7、整数反转

class Solution {public int reverse(int x) {int rev 0;while (x ! 0) {if (rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10) {return 0;}int digit x % 10;x / 10;rev rev * 10 digit;}return rev;} }

存储管理详解

目录 存储管理&#xff08;1&#xff09; 第一节 存储管理概述&#xff08;内存管理&#xff09; 一、存储体系 二、存储管理的任务 三、地址转换 存储管理&#xff08;2&#xff09; 第二节 分区管理方案 一、固定分区 二、可变分区 三、分区管理方案的优缺点 第…

C++ - 红黑树 介绍 和 实现

前言 前面 学习了 AVL树&#xff0c;AVL树虽然在 查找方面始终拥有 O(log N &#xff09;的极高效率&#xff0c;但是&#xff0c;AVL 树在插入 ,删除等等 修改的操作当中非常的麻烦&#xff0c;尤其是 删除操作&#xff0c;在实现当中细节非常多&#xff0c;在实现上非常难掌控…

git reset origin --hard解决‘Your branch is ahead of ‘origin/xxxx‘ by xx commit.’

git reset origin --hard解决‘Your branch is ahead of origin/xxxx by xx commit.’ 如图&#xff1a; 之前是这么解决的解决git&#xff1a;Your branch is ahead of ‘XXX‘ by X commits-CSDN博客git删除/撤销远已经push到程服务器上某次代码提交场景&#xff1a;不小心把…

【算法思想-排序】按出现频率排序 - 力扣 1636

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kuan 的首页,持续学…

无需申请专线、无需改动网络,ERP/MES管理系统如何远程访问?

深圳市某模具公司作为一家以设计、制作五金模具、五金冲压、机加工件、加工经营为主的五金企业。为了实现更为高效的生产管理流程&#xff0c;引入了面向钣金/五金行业信息化建设的ERP/MES管理系统及方案&#xff0c;并将其部署在了企业总部的内网服务器。 除了总部访问需求外&…