python网络爬虫之如何伪装逃过反爬虫程序的方法
作者:Master HaKu 时间:2022-08-12 10:23:23
有的时候,我们本来写得好好的爬虫代码,之前还运行得Ok, 一下子突然报错了。
报错信息如下:
Http 800 Internal internet error
这是因为你的对象网站设置了反爬虫程序,如果用现有的爬虫代码,会被拒绝。
之前正常的爬虫代码如下:
from urllib.request import urlopen
...
html = urlopen(scrapeUrl)
bsObj = BeautifulSoup(html.read(), "html.parser")
这个时候,需要我们给我们的爬虫代码做下伪装,
给它添加表头伪装成是来自浏览器的请求
修改后的代码如下:
import urllib.parse
import urllib.request
from bs4 import BeautifulSoup
...
req = urllib.request.Request(scrapeUrl)
req.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)')
response = urllib.request.urlopen(req)
html = response.read()
bsObj = BeautifulSoup(html, "html.parser")
Ok,一切搞定,又可以继续爬了。
来源:https://www.cnblogs.com/davidgu/p/5572547.html
标签:python,反爬虫
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python中k-means和k-means++原理及实现
2022-01-17 10:52:07
![](https://img.aspxhome.com/file/2023/7/66387_0s.png)
tensorflow使用指定gpu的方法
2022-10-23 16:00:31
python 多线程中子线程和主线程相互通信方法
2021-05-13 03:56:44
Pytorch之view及view_as使用详解
2023-10-09 23:10:34
Golang巧用defer进行错误处理的方法
2023-08-05 03:21:13
搜索关键字加亮js实现方法
2007-08-27 14:11:00
Vue父子组建的简单通信之控制开关Switch的实现
2024-06-05 09:16:25
![](https://img.aspxhome.com/file/2023/8/123058_0s.gif)
Python数据分析matplotlib设置多个子图的间距方法
2021-03-16 01:08:29
最新Python idle下载、安装与使用教程图文详解
2022-08-27 12:33:15
![](https://img.aspxhome.com/file/2023/9/125149_0s.png)
巧用Dreamweaver MX制作阴影字
2009-08-31 17:36:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113255757s.gif)
教会你完全搞定MySQL数据库 轻松八句话
2024-01-26 22:34:53
SQL Server Bulk Insert 只需要部分字段时的方法
2011-10-24 19:44:49
深入探讨JavaScript、JQuery屏蔽网页鼠标右键菜单及禁止选择复制
2024-06-23 09:05:25
REPAIR TABLE语法介绍——MySQL数据库
2012-01-05 19:08:59
浅析python 定时拆分备份 nginx 日志的方法
2023-08-04 06:03:37
Python模仿POST提交HTTP数据及使用Cookie值的方法
2022-05-04 04:37:35
Orcas中C#语言的新特性:自动属性,对象初始化器,和集合初始化器
2007-09-23 12:43:00
Python中format()格式输出全解
2022-09-01 11:10:34
![](https://img.aspxhome.com/file/2023/2/128802_0s.png)
详解MySQL双活同步复制四种解决方案
2024-01-13 04:39:47
![](https://img.aspxhome.com/file/2023/7/106607_0s.jpg)
解决ele ui 表格表头太长问题的实现
2024-05-13 09:44:00
![](https://img.aspxhome.com/file/2023/5/125275_0s.png)