RT,不用 headless的話,要彈出視窗,但是保存的資料是完整的,用 headless的話保存的資料就沒有。如果換firefox無頭模式的話又正常了。
什么原因呢?
import json
from bs4 import BeautifulSoup
import os
import sqlite3
from win32.win32crypt import CryptUnprotectData
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def getcookiefromchrome(host):
cookiepath=os.environ['LOCALAPPDATA']+r"\Google\Chrome\User Data\Default\Cookies"
sql="select host_key,name,encrypted_value from cookies where host_key='%s'" % host
with sqlite3.connect(cookiepath) as conn:
cu=conn.cursor()
cookies={name:CryptUnprotectData(encrypted_value)[1].decode() for host_key,name,encrypted_value in cu.execute(sql).fetchall()}
#print(cookies)
return cookies
host='192.168.205.186'
surl=r"http://192.168.205.186:50000/manufacturing/index.jsp"
starturl =r"http://192.168.205.186:50000/shineraywebwar/application/setup/SfcObtainReport"
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36'
}
searchdata = {
'P_START':'2019-12-18+0%3A00%3A00',
'P_END':'2019-12-20+0%3A00%3A00',
'OPERATION':'AM160',
'CAR_TYPE':'',
'SITE':'2000',
'ACTIVITY_ID':'Z_PP_SFC_OBTAIN',
'_FORM_NON_KEY_MODIFIED':'true',
'DISTINCT_BOX':'false',
'DISTRIBUTION_DETAIL_TABLE_SELECTED_ROW_INDEX':'-1',
'LINE':'',
'P_START_END_OF_DAY':'false',
'P_END_END_OF_DAY':'false',
'P_END_LOCALE_CONTEXT':'',
'P_END_SHOW_TIME':'true',
'P_START_LOCALE_CONTEXT':'',
'P_START_SHOW_TIME':'true',
'RESOURCE':'',
'USER_CMD':'RetrieveCommand',
'VIN_NUMBER':'',
}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options = chrome_options)
#driver = webdriver.Chrome()#有頭模式
driver.get(surl)
driver.delete_all_cookies()
cookies=getcookiefromchrome(host)
for i in cookies:
driver.add_cookie({'name':i,'value':cookies[i]})
driver.get(starturl)
bs = BeautifulSoup(driver.page_source,"html.parser")
for i in bs.find_all('input',type='hidden'):
if i.get('name')=='APP_ID':
searchdata['APP_ID']=i["value"]
elif i.get('name')=='FORM_ID':
searchdata['FORM_ID']=i["value"]
break
aa=''
for ii in searchdata:
aa=aa+ii+'='+searchdata[ii]+'&'
starturl=starturl+'?'+aa
print(starturl[:-1])
starturl=starturl[:-1]
driver.get(starturl)
bs = BeautifulSoup(driver.page_source,"html.parser")
ss=bs.find(id="DISTRIBUTION_DETAIL_TABLE")
print(ss)
open('contacts.html', 'w+', encoding='utf-8').write(driver.page_source)
driver.quit()
driver.close()


轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/112302.html
下一篇:python問題
