day3-代理和selenium

代理和IP的使用

import requests


# 1. 获取蘑菇代理中的代理IP
def get_ip():
    response = requests.get('http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=775206edf3dc4329ba04568b75a66a30&count=4&expiryDate=0&format=2&newLine=3')
    if response.text[0] == '{':
        print('提取IP失败')
        return None
    return [x for x in response.text.split('\n') if x]


# 2. 使用代理IP
def get_net_data():
    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
    }
    # 'http': 'ip:端口'、'http': 'http://ip:端口'
    # 'https': 'ip:端口'、'https': 'https://ip:端口'
    ips = get_ip()
    # 判断是否提取到有效ip
    if not ips:
        print('ip获取失败,等10秒以后重新运行')
        return

    proxies = {
        'http': ips[0],
        'https': ips[1]
    }
    response = requests.get('https://movie.douban.com/top250', headers=headers, proxies=proxies)
    print(response.text)


if __name__ == '__main__':
    get_net_data()

使用代理和优化程序

import requests
import time


def get_ip():
    response = requests.get('http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=775206edf3dc4329ba04568b75a66a30&count=4&expiryDate=0&format=2&newLine=3')
    if response.text[0] == '{':
        print('提取IP失败')
        return None
    return [x for x in response.text.split('\n') if x]


def get_net_data():
    # 不断获取ip直到成功
    while True:
        ips = get_ip()
        if ips:
            break
        time.sleep(5)
    print('ip获取成功:', ips)

    headers = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36'
    }
    proxies = {
        'http': ips[0],
        'https': ips[1]
    }
    url = 'https://movie.douban.com/top250'
    response = requests.get(url, headers=headers, proxies=proxies)
    print(response.text)


get_net_data()

selenium的基本功能

from selenium.webdriver import Chrome

# 1. 创建浏览器对象(如果是全局变量,程序结束浏览器不会关闭;局部变量会自动关闭)
b = Chrome()

# 2. 输入网址
b.get('https://www.jd.com/')

# 3. 获取网页源代码
print(b.page_source)

# 关闭浏览器
# b.close()

selenium常规交互

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time

# 1. 创建浏览器
b = Chrome()
# 2. 打开网页
b.get('https://www.51job.com/')

# 3. 获取标签(输入框)
# search_input = b.find_element_by_id('kwdselectid')
search_input = b.find_element_by_css_selector('#kwdselectid')
# print(search_input)

# 4.在输入框中输入内容
search_input.send_keys('数据分析')
# 按输入框中按回车键
search_input.send_keys(Keys.ENTER)

# 5.获取网页数据
print(b.page_source)
print('--------------------------------------------------------------------------------------------')
# 6. 获取下一页对应的标签
next = b.find_element_by_css_selector('.next')

# 7.点击按钮
next.click()
print('+++++++++++++++++')
time.sleep(1)
print(b.page_source)

selenium常用配置

from selenium.webdriver import Chrome, ChromeOptions
import requests
import time


def get_ip():
    response = requests.get('http://piping.mogumiao.com/proxy/api/get_ip_bs?appKey=775206edf3dc4329ba04568b75a66a30&count=4&expiryDate=0&format=2&newLine=3')
    if response.text[0] == '{':
        print('提取IP失败')
        return None
    return [x for x in response.text.split('\n') if x]


while True:
    ips = get_ip()
    if ips:
        break
    time.sleep(1)
print(ips)

# 1. 创建谷歌浏览器的配置对象
options = ChromeOptions()
# 1) 添加取消测试环境选项
options.add_experimental_option('excludeSwitches', ['enable-automation'])
# 2) 取消图片加载
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
# # 3) 设置代理
# options.add_argument(f'--proxy-server=http://{ips[0]}')

b = Chrome(options=options)
b.get('https://movie.douban.com/top250')
print(b.page_source)

爬淘宝

from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.keys import Keys

options = ChromeOptions()
options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})

# 用浏览器打开网页
b = Chrome(options=options)
b.get('https://www.taobao.com')

# 设置cookies
cookies = eval(open('files/taobao.txt', encoding='utf-8').read())
for cookie in cookies:
    if cookie['secure']:
        b.add_cookie(cookie)

b.get('https://www.taobao.com')
search_input = b.find_element_by_id('q')
search_input.send_keys('鞋子')
search_input.send_keys(Keys.ENTER)

print(b.page_source)

获取和保存cookie值

from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
import time


def save_cookie():
    # 打开浏览器,引导到登录页面
    b = Chrome()
    b.get('https://www.taobao.com')
    search_input = b.find_element_by_id('q')
    search_input.send_keys('鞋子')
    search_input.send_keys(Keys.ENTER)
    # 人工做登录操作(给足够的时间)
    time.sleep(10)
    # input('是否继续:')

    # 获取cookie
    cookies = b.get_cookies()
    # print(cookies, type(cookies))
    f = open('files/taobao.txt', 'w', encoding='utf-8')
    f.write(str(cookies))
    b.quit()

save_cookie()

爬51json数据分析

from selenium.webdriver import Chrome
from bs4 import BeautifulSoup
import csv
from selenium.webdriver.common.keys import Keys
import re
import time, json

f = open('files/数据分析.csv', 'a', encoding='utf-8')
writer = csv.writer(f)
writer.writerow(['岗位', '薪资', '公司', '地址'])


b = Chrome()


def get_net_data():

    b.get('https://www.51job.com/')
    search_input = b.find_element_by_css_selector('#kwdselectid')
    search_input.send_keys('数据分析')
    search_input.send_keys(Keys.ENTER)

    while True:
        # 获取页信息
        page_div = b.find_element_by_css_selector('.rt.rt_page')
        pages = page_div.text.split('/')
        all_page = int(pages[-1])
        current_page = int(pages[0])

        # 获取网页源代码
        # print(b.page_source)
        save_data(b.page_source)

        if current_page < 10:
            next = b.find_element_by_css_selector('.next')
            next.click()
        else:
            break


def save_data(html: str):
    soup = BeautifulSoup(html, 'lxml')
    all_job_div = soup.select('.j_joblist>.e')
    one_page_jobs = []
    for job_div in all_job_div:
        name = job_div.select_one('.jname.at').get_text()
        sal = job_div.select_one('.sal').get_text()
        company = job_div.select_one('.cname.at').get_text()
        job_url = job_div.select_one('.el').attrs['href']
        one_page_jobs.append([name, sal, company, job_url])
    writer.writerows(one_page_jobs)


if __name__ == '__main__':
    get_net_data()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【为什么学爬虫?】        1、爬虫入手容易,但是深入较难,如何写出高效率的爬虫,如何写出灵活性高可扩展的爬虫都是一项技术活。另外在爬虫过程中,经常容易遇到被反爬虫,比如字体反爬、IP识别、验证码等,如何层层攻克难点拿到想要的数据,这门课程,你都能学到!        2、如果是作为一个其他行业的开发者,比如app开发,web开发,学习爬虫能让你加强对技术的认知,能够开发出更加安全的软件和网站 【课程设计】 一个完整的爬虫程序,无论大小,总体来说可以分成三个步骤,分别是:网络请求:模拟浏览器的行为从网上抓取数据。数据解析:将请求下来的数据进行过滤,提取我们想要的数据。数据存储:将提取到的数据存储到硬盘或者内存中。比如用mysql数据库或者redis等。那么本课程也是按照这几个步骤循序渐进的进行讲解,带领学生完整的掌握每个步骤的技术。另外,因为爬虫的多样性,在爬取的过程中可能会发生被反爬、效率低下等。因此我们又增加了两个章节用来提高爬虫程序的灵活性,分别是:爬虫进阶:包括IP代理,多线程爬虫,图形验证码识别、JS加密解密、动态网页爬虫、字体反爬识别等。Scrapy和分布式爬虫:Scrapy框架、Scrapy-redis组件、分布式爬虫等。通过爬虫进阶的知识点我们能应付大量的反爬网站,而Scrapy框架作为一个专业的爬虫框架,使用他可以快速提高我们编写爬虫程序的效率和速度。另外如果一台机器不能满足你的需求,我们可以用分布式爬虫让多台机器帮助你快速爬取数据。 从基础爬虫到商业化应用爬虫,本套课程满足您的所有需求!【课程服务】 专属付费社群+定期答疑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值