Python * 爬虫的新手使用教程
作者:lxiaok 发布时间:2021-09-27 09:27:59
前言
Python爬虫要经历爬虫、爬虫被限制、爬虫反限制的过程。当然后续还要网页爬虫限制优化,爬虫再反限制的一系列道高一尺魔高一丈的过程。爬虫的初级阶段,添加headers和ip代理可以解决很多问题。
本人自己在爬取豆瓣读书的时候,就以为爬取次数过多,直接被封了IP.后来就研究了 * 的问题.
(当时不知道什么情况,差点心态就崩了...),下面给大家介绍一下我自己 * 爬取数据的问题,请大家指出不足之处.
问题
这是我的IP被封了,一开始好好的,我还以为是我的代码问题了
思路:
从网上查找了一些关于爬虫 * 的资料,得到下面的思路
爬取一些IP,过滤掉不可用.
在requests的请求的proxies参数加入对应的IP.
继续爬取.
收工
好吧,都是废话,理论大家都懂,上面直接上代码...
思路有了,动手起来.
运行环境
Python 3.7, Pycharm
这些需要大家直接去搭建好环境...
准备工作
爬取IP地址的网站(国内高匿代理)
校验IP地址的网站
你之前被封IP的py爬虫脚本...
上面的网址看个人的情况来选取
爬取IP的完整代码
PS:简单的使用bs4获取IP和端口号,没有啥难度,里面增加了一个过滤不可用IP的逻辑
关键地方都有注释了
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018/11/22
# @Author : liangk
# @Site :
# @File : auto_archive_ios.py
# @Software: PyCharm
import requests
from bs4 import BeautifulSoup
import json
class GetIp(object):
"""抓取 * """
def __init__(self):
"""初始化变量"""
self.url = 'http://www.xicidaili.com/nn/'
self.check_url = 'https://www.ip.cn/'
self.ip_list = []
@staticmethod
def get_html(url):
"""请求html页面信息"""
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
try:
request = requests.get(url=url, headers=header)
request.encoding = 'utf-8'
html = request.text
return html
except Exception as e:
return ''
def get_available_ip(self, ip_address, ip_port):
"""检测IP地址是否可用"""
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36'
}
ip_url_next = '://' + ip_address + ':' + ip_port
proxies = {'http': 'http' + ip_url_next, 'https': 'https' + ip_url_next}
try:
r = requests.get(self.check_url, headers=header, proxies=proxies, timeout=3)
html = r.text
except:
print('fail-%s' % ip_address)
else:
print('success-%s' % ip_address)
soup = BeautifulSoup(html, 'lxml')
div = soup.find(class_='well')
if div:
print(div.text)
ip_info = {'address': ip_address, 'port': ip_port}
self.ip_list.append(ip_info)
def main(self):
"""主方法"""
web_html = self.get_html(self.url)
soup = BeautifulSoup(web_html, 'lxml')
ip_list = soup.find(id='ip_list').find_all('tr')
for ip_info in ip_list:
td_list = ip_info.find_all('td')
if len(td_list) > 0:
ip_address = td_list[1].text
ip_port = td_list[2].text
# 检测IP地址是否有效
self.get_available_ip(ip_address, ip_port)
# 写入有效文件
with open('ip.txt', 'w') as file:
json.dump(self.ip_list, file)
print(self.ip_list)
# 程序主入口
if __name__ == '__main__':
get_ip = GetIp()
get_ip.main()
使用方法完整代码
PS: 主要是通过使用随机的IP来爬取,根据request_status来判断这个IP是否可以用.
为什么要这样判断?
主要是虽然上面经过了过滤,但是不代表在你爬取的时候是可以用的,所以还是得多做一个判断.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : 2018/11/22
# @Author : liangk
# @Site :
# @File : get_douban_books.py
# @Software: PyCharm
from bs4 import BeautifulSoup
import datetime
import requests
import json
import random
ip_random = -1
article_tag_list = []
article_type_list = []
def get_html(url):
header = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36'
}
global ip_random
ip_rand, proxies = get_proxie(ip_random)
print(proxies)
try:
request = requests.get(url=url, headers=header, proxies=proxies, timeout=3)
except:
request_status = 500
else:
request_status = request.status_code
print(request_status)
while request_status != 200:
ip_random = -1
ip_rand, proxies = get_proxie(ip_random)
print(proxies)
try:
request = requests.get(url=url, headers=header, proxies=proxies, timeout=3)
except:
request_status = 500
else:
request_status = request.status_code
print(request_status)
ip_random = ip_rand
request.encoding = 'gbk'
html = request.content
print(html)
return html
def get_proxie(random_number):
with open('ip.txt', 'r') as file:
ip_list = json.load(file)
if random_number == -1:
random_number = random.randint(0, len(ip_list) - 1)
ip_info = ip_list[random_number]
ip_url_next = '://' + ip_info['address'] + ':' + ip_info['port']
proxies = {'http': 'http' + ip_url_next, 'https': 'https' + ip_url_next}
return random_number, proxies
# 程序主入口
if __name__ == '__main__':
"""只是爬取了书籍的第一页,按照评价排序"""
start_time = datetime.datetime.now()
url = 'https://book.douban.com/tag/?view=type&icn=index-sorttags-all'
base_url = 'https://book.douban.com/tag/'
html = get_html(url)
soup = BeautifulSoup(html, 'lxml')
article_tag_list = soup.find_all(class_='tag-content-wrapper')
tagCol_list = soup.find_all(class_='tagCol')
for table in tagCol_list:
""" 整理分析数据 """
sub_type_list = []
a = table.find_all('a')
for book_type in a:
sub_type_list.append(book_type.text)
article_type_list.append(sub_type_list)
for sub in article_type_list:
for sub1 in sub:
title = '==============' + sub1 + '=============='
print(title)
print(base_url + sub1 + '?start=0' + '&type=S')
with open('book.text', 'a', encoding='utf-8') as f:
f.write('\n' + title + '\n')
f.write(url + '\n')
for start in range(0, 2):
# (start * 20) 分页是0 20 40 这样的
# type=S是按评价排序
url = base_url + sub1 + '?start=%s' % (start * 20) + '&type=S'
html = get_html(url)
soup = BeautifulSoup(html, 'lxml')
li = soup.find_all(class_='subject-item')
for div in li:
info = div.find(class_='info').find('a')
img = div.find(class_='pic').find('img')
content = '书名:<%s>' % info['title'] + ' 书本图片:' + img['src'] + '\n'
print(content)
with open('book.text', 'a', encoding='utf-8') as f:
f.write(content)
end_time = datetime.datetime.now()
print('耗时: ', (end_time - start_time).seconds)
为什么选择国内高匿代理!
总结
使用这样简单的 * ,基本上就可以应付在爬爬爬着被封IP的情况了.而且没有使用自己的IP,间接的保护?!?!
大家有其他的更加快捷的方法,欢迎大家可以拿出来交流和讨论,谢谢。
好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。
来源:https://juejin.im/post/5bf66ef56fb9a04a053f4fc1
猜你喜欢
- 自从2005年8月11日阿里巴巴宣布收购雅虎中国的全部资产后,做为阿里巴巴集团的创始人马云心里盘算的事应该是如何把雅虎中虎整合进阿里巴巴这个
- 生成方式Python中想要自动生成 model文件可以通过 sqlacodegen这个命令来生成对应的model文件sqlacodegen
- 本文实现12306抢火车票/京东抢手机示例,具体如下:#12306秒抢Python代码from splinter.browser impor
- 1.创建tfrecordtfrecord支持写入三种格式的数据:string,int64,float32,以列表的形式分别通过tf.trai
- 在做DHTML时,我们在某些情况下要用setAttribute(attri, value)方法定义元素的attribute。同时与getAt
- 正在看的ORACLE教程是:Oracle数据库集复制方法浅议。前言 日益增长的分布式应用需求要求实现更好分布式的软件环境,不断推动着分布式技
- With语句是什么?有一些任务,可能事先需要设置,事后做清理工作。对于这种场景,Python的with语句提供了一种非常方便的处理方式。一个
- 目前lnmp一键安装包比较多的问题就是502 Bad Gateway,大部分情况下原因是在安装php前,脚本中某些lib包可能没有安装上,造
- 近日,朋友写一个关于成绩管理的系统,其中遇到一个小问题。如果按照SQLSERVER的ORDER 排序时,比如遇到两个100分,结果必然是名次
- 大多数程序员考虑编程时,他们都要设想用于编写应用程序的 命令式样式和技术。最受欢迎的通用编程语言(包括 Python 和其它面向对象的语言)
- 如下所示:import matplotlib.pyplot as pltimport numpy as npdef readfile(fil
- 1 简介DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表。或许说它可能有点像matlab的矩
- Javascript 正常取来源网页的URL只要用: document.referrer就可以了!但,如果来源页是Jav
- 导读只需要添加几行代码,就可以得到更快速,更省显存的PyTorch模型。你知道吗,在1986年Geoffrey Hinton就在Nature
- 本文实例为大家分享了Python实点云分割k-means(sklearn),供大家参考,具体内容如下植物叶片分割import numpy a
- 说来惭愧,以前在去掉数组的空值是都是强写foreach或者while的,利用这两个语法结构来删除数组中的空元素,简单代码如下:<?ph
- win2000注册表程序 regedt32.exe下面是解决IIS出现Active Server Pages错误&
- Python内置函数isdigit()使用今天简单介绍一下Python中的isdigit()函数的用法:判断单个字符是否为数字判断字符串中是
- 目录1. pgzeropgzero的安装2. 游戏设计的过程3. pgzero基础4. pgzero游戏例子5. 总结6. 参考资料1. p
- 本文实例讲述了python使用socket连接远程服务器的方法。分享给大家供大家参考。具体如下:import socketprint &qu