selenium模拟使用

版权声明:个人原创,所属@jerry 本人 https://blog.csdn.net/qq_42938842/article/details/83989942

selenium+phantomjs
selenium是什么?是一个浏览器的自动化测试工具,就是通过写代码去操作浏览器,让浏览器做一些自动化的工作
selenium如何操作谷歌浏览器
安装selenium,pip install selenium
步骤:selenium操作谷歌浏览器,其实是操作谷歌浏览器的驱动,由驱动再去驱动浏览器
谷歌浏览器驱动下载地址
http://chromedriver.storage.googleapis.com/index.html
http://npm.taobao.org/mirrors/chromedriver/
http://blog.csdn.net/huilan_same/article/details/51896672
headlesschrome
phantomjs是无界面浏览器
谷歌无界面模式
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument(’–headless’)
chrome_options.add_argument(’–disable-gpu’)
selenium驱动火狐浏览器
下载火狐驱动
https://github.com/mozilla/geckodriver/releases
版本映射
https://blog.csdn.net/yinshuilan/article/details/79730239
firefox_options = webdriver.FirefoxOptions()
firefox_options.set_headless()
firefox_options.add_argument(’–disable-gpu’)
在这里我爬取的是JD,代码如下:

from lxml import etree
from selenium import webdriver
import json
import time
from selenium.webdriver.chrome.options import Options



    # 获取没页面内容
def save_content(driver, fp):

    response1 = driver.page_source
    tree1 = etree.HTML(response1)
    a_href_list = tree1.xpath('//ul[@class="gl-warp clearfix"]/li//div[@class="p-img"]/a/@href')
    chrome_options1 = Options()
    chrome_options1.add_argument('--headless')
    chrome_options1.add_argument('--disable-gpu')
    driver1 = webdriver.Chrome(executable_path=r'F:\chromedriver.exe',
                              chrome_options=chrome_options1)
    for a_href in a_href_list:
        a_href = 'http:' + a_href
        driver1.get(url=a_href)
        response2 = driver1.page_source
        tree2 = etree.HTML(response2)
        # get_attribute 获取属性
        # text 获取文本
        C_name = driver1.find_element_by_xpath('.//div[@class="sku-name"]').text  # 获取电脑基本信息
        C_price = driver1.find_element_by_xpath('.//div[@class="dd"]/span/span[2]').text  # 获取电脑报价
        C_style = driver1.find_element_by_xpath('.//div[@id="store-prompt"]/strong').text  # 获取商品状态
        C_image = driver1.find_element_by_xpath('.//div[@id="preview"]//img').get_attribute(
                    'src')  # 获取电脑图片
        # 商品品牌
        C_brand = tree2.xpath('//ul[@id="parameter-brand"]/li/@title')[0]
        # 商品编号
        C_styleid = tree2.xpath('//ul[@class="parameter2 p-parameter-list"]/li[2]/text()')[0].strip('商品编号:')
        # 商品产地
        C_origin = tree2.xpath('//ul[@class="parameter2 p-parameter-list"]/li[4]/text()')[0].strip('商品产地:')
        item = {
                '商品图片': C_image,
                '商品信息概括': C_name,
                '商品品牌': C_brand,
                '商品价格': C_price,
                '商品编号': C_styleid,
                '商品产地': C_origin,
                '商品状态': C_style,
        }
        string = json.dumps(item, ensure_ascii=False)
        fp.write(string + '\n')
        print('正在下载%s' % C_name)
def run():
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(executable_path=r'F:\第四阶段\day06\day06_pm\ziliao\chromedriver.exe',
                                   chrome_options=chrome_options)
    driver.get("https://list.jd.com/list.html?cat=670%2C671%2C672&go=0")
    time.sleep(3)  # 每次发送完请求等待三秒,等待页面加载完成
    # 请求首页
    # 1.发送首页的请求
    # 2.获取第一页的信息
    fp = open("jd.txt", "w", encoding='utf8')
    # 保存内容
    save_content(driver,fp)
    # 3.循环  点击下一页按钮,知道下一页对应的class名字不再是"pn-next"
    while driver.find_element_by_class_name("pn-next"):  # 判断有没有下一页
        # 点击下一页的按钮
        driver.find_element_by_class_name("pn-next").click()  #
        # 4.继续获取下一页的内容,保存内容
        save_content(driver,fp)
    # 走到这认为没有下一页,关闭文件
    fp.close()


if __name__ == "__main__":
    run()

猜你喜欢

转载自blog.csdn.net/qq_42938842/article/details/83989942