selenium 爬取cookie并且把数据下载到Excel

selenium 爬取cookie并且把数据下载到Excel

import requests
import re
import csv
import ftplib
import os
import sys
import datetime
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
import pandas as pd
from bs4 import BeautifulSoup
import numpy as np


#获取cookie
# 实例化一个启动参数对象
chrome_options = Options()
# 无界面运行
chrome_options.add_argument('--headless')
#Disables GPU hardware acceleration. If software renderer is not in place, then the GPU process won't launch.
chrome_options.add_argument('--disable-gpu')
path = (r'C:\ProgramData\Anaconda3\chromedriver.exe')
opener = webdriver.Chrome(executable_path=path)
opener.maximize_window()      #设置全屏
opener.get('http://erp.jetcloudmall.com:91/')    #打开网站
opener.find_element_by_xpath('//*[@id="form-username"]').send_keys('1962')     #输入账号 检查copy Xpath
opener.find_element_by_xpath('//*[@id="form-password"]').send_keys('a112')    #输入密码
opener.find_element_by_xpath('/html/body/div/div/div/div[2]/div/div[2]/form/button').click()     #点击登陆
time.sleep(5)        #睡眠5秒
#opener.find_element_by_xpath('//*[@id="side-menu"]/li[9]/a/span[1]').click()   #点击
#time.sleep(2)
#opener.find_element_by_xpath('//*[@id="side-menu"]/li[9]/ul/li[2]/a').click()       #点击
#time.sleep(3)
cookies = opener.get_cookies()#获取cookies
name_pat = "'name':.'(.*?)'"
value_pat = "'value':.'(.*?)'"
value = re.compile(value_pat).findall(str(cookies)) #compile匹配 findall找到
name = re.compile(name_pat).findall(str(cookies))
time.sleep(2)
opener.quit()   #关闭网页
#提取组合cookie
list = []
for i in range(0,len(name)):
    list1 = name[i]+'='+value[i]
    list.append(list1)
print(list)
result = list[2] + '; ' + list[1]
print(result)

my_headers1={"Cookie":result,
            "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36",
            }
my_data1={"page":"1",
         "pageSize":"3000",
         "filter":"BeginDate~eq~'2019-04-05'~and~EndDate~eq~'2019-05-05'",
         }
#时间处理
now = datetime.datetime.now()
end_time = datetime.datetime.strftime(now, '%Y-%m-%d')
begin_time = now+ datetime.timedelta(days=-90)
begin_time1 = begin_time.strftime('%Y-%m-%d')
#下读取载数据链接
url='http://erp.jetcloudmall.com:91/PlanDeptReport/SKUDeliverySummaryDownload?a=1&BeginDate='+str(begin_time1)+'&EndDate='+str(end_time)+''
jiao = requests.get(url,data=my_data1,headers=my_headers1)

#删除读取写入
try:
    os.remove('平均交期.xlsx')
except:
    pass
with open("平均交期.xlsx", "wb") as code:#wb是二进制写入
    code.write(jiao.content)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
# -*- coding:utf-8 -*- import sys #print (u'系统默认编码为',sys.getdefaultencoding()) default_encoding = 'utf-8' #重新设置编码方式为uft-8 if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) #print (u'系统默认编码为',sys.getdefaultencoding()) import requests from bs4 import BeautifulSoup import traceback import re import xlwt def getURLDATA(url): #url = 'http://www.cnnvd.org.cn/web/xxk/ldxqById.tag?CNNVD=CNNVD-201901-1014' header={ 'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36', 'Connection': 'keep-alive',} r=requests.get(url,headers=header,timeout=30) #r.raise_for_status()抛出异常 html = BeautifulSoup(r.content.decode(),'html.parser') link=html.find(class_='detail_xq w770')#漏洞信息详情 link_introduce=html.find(class_='d_ldjj')#漏洞简介 link_others=html.find_all(class_='d_ldjj m_t_20')#其他 #print(len(link_introduce)) try: #print ("危害等级:"+link.contents[3].contents[3].find('a').text.lstrip().rstrip())#危害等级 list4.append(str(link.contents[3].contents[3].find('a').text.lstrip().rstrip())) except: #print("危害等级:is empty") list4.append("") try: #print ("CVE编号:"+link.contents[3].contents[5].find('a').text.lstrip().rstrip())#CVE编号 list5.append(str(link.contents[3].contents[5].find('a').text.lstrip().rstrip())) except: #print("CVE编号:is empty") list5.append("") try: #print ("漏洞类型:"+link.contents[3].contents[7].find('a').text.lstrip().rstrip())#漏洞类型 list6.append(str(link.contents[3].contents[7].find('a').text.lstrip().rstrip())) except : #print("漏洞类型:is empty") list6.append("") try: #print ("发布时间:"+link.contents[3].contents[9].find('a').text.lstrip().rstrip())#发布时间 list7.append(str(link.contents[3].contents[9].find('a').text.lstrip().rstrip())) except : #print("发布时间:is empty") list7.append("") try: #print ("威胁类型:"+link.contents[3].contents[11].find('a').text.lstrip().rstrip())#威胁类型 list8.append(str(link.contents[3].contents[11].find('a').text.lstrip().rstrip())) except : #print("威胁类型:is empty") list8.append("") try: #print ("更新时间:"+link.contents[3].contents[13].find('a').text.lstrip().rstrip())#更新时间 list9.append(str(link.contents[3].contents[13].find('a').text.lstrip().rstrip())) except : #print("更新时间:is empty") list9.append("") try: #print ("厂商:"+link.contents[3].contents[15].find('a').text.lstrip().rstrip())#厂商 list10.append(str(link.contents[3].contents[15].find('a').text.lstrip().rstrip())) except: #print("厂商:is empty") list10.append("") #link_introduce=html.find(class_='d_ldjj')#漏洞简介 try: link_introduce_data=BeautifulSoup(link_introduce.decode(),'html.parser').find_all(name='p') s="" for i in range(0,len(link_introduce_data)): ##print (link_introduce_data[i].text.lstrip().rstrip()) s=s+str(link_introduce_data[i].text.lstrip().rstrip()) #print(s) list11.append(s) except : list11.append("") if(len(link_others)!=0): #link_others=html.find_all(class_='d_ldjj m_t_20') #print(len(link_others)) try: #漏洞公告 link_others_data1=BeautifulSoup(link_others[0].decode(),'html.parser').find_all(name='p') s="" for i in range(0,len(link_others_data1)): ##print (link_others_data1[i].text.lstrip().rstrip()) s=s+str(link_others_data1[i].text.lstrip().rstrip()) #print(s) list12.append(s) except: list12.append("") try: #参考网址 link_others_data2=BeautifulSoup(link_others[1].decode(),'html.parser').find_all(name='p') s="" for i in range(0,len(link_others_data2)): ##print (link_others_data2[i].text.lstrip().rstrip()) s=s+str(link_others_data2[i].text.lstrip().rstrip()) #print(s) list13.append(s) except: list13.append("") try: #受影响实体 link_others_data3=BeautifulSoup(link_others[2].decode(),'html.parser').find_all('a',attrs={'class':'a_title2'}) s="" for i in range(0,len(link_others_data3)): ##print (link_others_data3[i].text.lstrip().rstrip()) s=s+str(link_others_data3[i].text.lstrip().rstrip()) #print(s) list14.append(s) except: list14.append("") try: #补丁 link_others_data3=BeautifulSoup(link_others[3].decode(),'html.parser').find_all('a',attrs={'class':'a_title2'}) s="" for i in range(0,len(link_others_data3)): ##print (link_others_data3[i].t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值