Python爬虫第三次任务
任务预览(2天)
3.1 安装selenium并学习
3.1.1安装selenium并学习。
3.1.2使用selenium模拟登陆163邮箱。
注:163邮箱直通点:https://mail.163.com/ 。
3.2 学习IP相关知识
3.2.1学习什么是IP,为什么会出现IP被封,如何应对IP被封的问题。
3.2.2抓取西刺代理,并构建自己的代理池。
注:西刺直通点:https://www.xicidaili.com/ 。
3.1 安装selenium并学习
3.1.1安装selenium并学习。
Selenium 是一个自动化测试工具,利用它我们可以驱动浏览器执行特定的动作,如点击、下拉等等操作,对于一些 JavaScript 渲染的页面来说,此种抓取方式非常有效,下面我们来看下 Selenium 的安装过程。
安装:
可以直接使用pip安装:
pip3 install selenium
安装之后,需要验证安装:
可以导入一下selenium包,如果没有报错,则证明安装成功。
import selenium
但这样还不够,我们还需要浏览器如 Chrome、Firefox 等浏览器来配合 Selenium 工作。这里,我只介绍Chrome如何配合selenium,其他有兴趣可以自行百度
首先需要下载一个 Chrome 浏览器。
随后我们需要安装一个 ChromeDriver 才能驱动 Chrome 浏览器完成相应的操作,下面我们来介绍下怎样安装 ChromeDriver。
第一步:查看浏览器版本
点击 Chrome 的菜单,帮助->关于 Chrome,即可查看 Chrome 的版本号
第二步:下载ChromeDriver
打开 ChromeDriver 的官方网站,链接为:https://sites.google.com/a/chromium.org/chromedriver/downloads。
下载自己对应的版本
ChromeDriver版本 | Support Chrome版本 |
---|---|
2.46 | V71-73 |
2.45 | V70-72 |
2.44 | V69-71 |
2.43 | V69-71 |
2.42 | V68-70 |
2.41 | V67-69 |
2.40 | V66-68 |
2.39 | V66-68 |
2.38 | V65-67 |
2.37 | V64-66 |
2.36 | V63-65 |
2.35 |
也可以去镜像网站下载https://chromedriver.storage.googleapis.com/index.html
3.1.2使用selenium模拟登陆163邮箱。
由于账号密码登录的输入框在iframe中,所以我们先用switch_to.frame()切换到iframe中,在通过获取节点的方式获取账号的input、密码的input、登录按钮。
from time import sleep from selenium import webdriver ''' email是邮箱信息; password是密码信息 ''' def login(email, password): URL = "http://mail.163.com/" browser = webdriver.Chrome() browser.get(URL) sleep(5) browser.switch_to.frame(0) input_email = browser.find_element_by_name("email") #获取填邮箱的input input_email.send_keys(email) #填入邮箱信息 input_name = browser.find_element_by_name("password") #获取密码的input input_name.send_keys(password) #填入密码信息 button_login = browser.find_element_by_id("dologin") #获取登录按钮 button_login.click() #点击登录按钮 if __name__ == '__main__': email = input("请输入你的邮箱账号:(可省略后缀)") password = input("请输入你的密码:") login(email, password)
3.2 学习IP相关知识
3.2.1学习什么是IP,为什么会出现IP被封,如何应对IP被封的问题。
什么是IP
IP是分配给用户上网使用的网际协议(英语:Internet Protocol, IP)的设备的数字标签。每个上网的人都有一个自己的IP。
为什么会出现IP被封
许多网站拥有防爬虫机制,为了防止一个IP突然在短时间内进行大量访问。
如何应对IP被封的问题
1.伪造User-Agent,cookies等
2.使用代理IP
3.在每次重复爬取之间设置一个随机时间间隔
3.2.2抓取西刺代理,并构建自己的代理池。
分析网页:
不难看出,ip地址的信息在<tr class=“odd”>和<tr class>中,所以我们可以抓取tr来获得这些ip的地址,端口,和协议。
但是,我在这里使用了另外一种方式,将抓取<td>,每8个<td>就是一组IP地址
import requests from bs4 import BeautifulSoup def get_html(url): HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ''(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} response = requests.get(url, headers=HEADERS) response.encoding = 'utf-8' if response.status_code == 200: return response.text return None def get_ip(text): ip_list = [] soup = BeautifulSoup(text, 'html.parser') ips = soup.find(id='ip_list').find_all('td') n = (int)(len(ips)/8) #通过td个数除以8得到有多少个ip for i in range(n): #通过IP的个数来推出哪些是ip,port,protocol ip = ips[8*i+1].text port = ips[8*i+2].text protocol = ips[8*i+5].text if protocol in ('HTTP', 'HTTPS', 'http', 'https'): ip_list.append(f'{protocol}://{ip}:{port}') return ip_list if __name__ == '__main__': url = 'https://www.xicidaili.com/' html = get_html(url) ip_list = get_ip(html) print(ip_list)
这次一共获得100个ip
使用代理,并检测IP是否有用
这里就是简单的将抓到的IP,去访问百度,如果能成功访问百度,则有用。
import requests from bs4 import BeautifulSoup def get_html(url): HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ''(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'} response = requests.get(url, headers=HEADERS) response.encoding = 'utf-8' if response.status_code == 200: return response.text return None def get_ip(text): ip_list = [] soup = BeautifulSoup(text, 'html.parser') ips = soup.find(id='ip_list').find_all('td') n = (int)(len(ips)/8) #通过td个数除以8得到有多少个ip for i in range(n): #通过IP的个数来推出哪些是ip,port,protocol ip = ips[8*i+1].text port = ips[8*i+2].text protocol = ips[8*i+5].text if protocol in ('HTTP', 'HTTPS', 'http', 'https'): ip_list.append(f'{protocol}://{ip}:{port}') return ip_list def check_ip(ip): url = 'http://www.baidu.com' user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36' headers = {'User-Agent': user_agent} proxies = {} if ip.startswith(('HTTPS', 'https')): proxies['https'] = ip else: proxies['http'] = ip try: r = requests.get(url, headers=headers, proxies=proxies, timeout=10) r.raise_for_status() r.encoding = r.apparent_encoding if r.status_code == 200: print('有效代理IP: ' + ip) return True else: print('无效代理IP: ' + ip) return False except: print('无效代理IP: ' + ip) return False if __name__ == '__main__': url = 'https://www.xicidaili.com/' html = get_html(url) ip_list = get_ip(html) print(ip_list) ip_use_list = [] for ip in ip_list: result = check_ip(ip) if(result): ip_use_list.append(ip) print("有效代理IP:") print(ip_use_list)
- 使用Python写的一个爬虫【任务队列版本】
- 预习任务:python 网络爬虫
- Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?
- 【原创】编写多线程Python爬虫来过滤八戒网上的发布任务
- python 每天如何定时启动爬虫任务
- Python学习 第一天任务 (三:Python不止基础学习 继续安装爬虫环境的配置)
- python 爬虫每天定时启动爬虫任务
- python 每天如何定时启动爬虫任务(实现方法分享)
- Python有了asyncio和aiohttp在爬虫这类型IO任务中多线程/多进程还有存在的必要吗?
- Ubuntu下配置python完成爬虫任务(笔记一)
- Python爬虫之Spark Streaming任务监控
- Python3 大型网络爬虫实战 004 — scrapy 大型静态商城网站爬虫项目编写及数据写入数据库实战 — 实战:爬取淘宝
- Python爬虫实战案例:爬取爱奇艺VIP视频
- 慕课实例Python爬虫中国大学排名爬虫总结与思考
- python简单爬虫示例
- Python爬虫——Python 岗位分析报告
- Python爬虫爬数据写入到文件
- python爬虫入门 实战(一)---爬糗事百科
- python爬虫和正则表达式
- python 爬虫基础知识,返回值为json数据的 爬虫原代码