在设置cookie这块我是头疼的

master
wkyuu 3 years ago
parent 742120dd05
commit 84b8de58b2

@ -208,4 +208,8 @@ redisconn = redis.Redis(host = '127.0.0.1', port = '6379', password = 'x', db =
11[如何理解Python装饰器- 知乎](https://www.zhihu.com/question/26930016/answer/360300235) 11[如何理解Python装饰器- 知乎](https://www.zhihu.com/question/26930016/answer/360300235)
12[【自动化】selenium设置请求头](https://www.jianshu.com/p/419eb4e00963) 12[【自动化】selenium设置请求头](https://www.jianshu.com/p/419eb4e00963)
13[https://blog.csdn.net/fox64194167/article/details/80542717](https://blog.csdn.net/fox64194167/article/details/80542717)
14

@ -1,10 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import json
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from lxml import etree from lxml import etree
import random import random
import settings import settings
import time
headers = { headers = {
'User-Agent': random.choice(settings.USER_AGENT) 'User-Agent': random.choice(settings.USER_AGENT)
@ -34,7 +36,7 @@ def getsource(url):
def manmanbuy(url): def manmanbuy(url):
initChrome = Options() initChrome = Options()
'''
initChrome.add_argument('--no-sandbox') initChrome.add_argument('--no-sandbox')
initChrome.add_argument('--headless') initChrome.add_argument('--headless')
initChrome.add_argument('--disable-gpu') initChrome.add_argument('--disable-gpu')
@ -42,8 +44,28 @@ def manmanbuy(url):
initChrome.add_argument('disable-infobars') initChrome.add_argument('disable-infobars')
initChrome.add_argument('log-level=3') # INFO = 0 WARNING = 1 LOG_ERROR = 2 LOG_FATAL = 3 default is 0 initChrome.add_argument('log-level=3') # INFO = 0 WARNING = 1 LOG_ERROR = 2 LOG_FATAL = 3 default is 0
initChrome.add_experimental_option("excludeSwitches",['enable-automation','enable-logging']) initChrome.add_experimental_option("excludeSwitches",['enable-automation','enable-logging'])
'''
driver = webdriver.Chrome(chrome_options = initChrome, executable_path = './chromedriver.exe') # driver = webdriver.Chrome(chrome_options = initChrome, executable_path = './chromedriver.exe')
driver = webdriver.Chrome(executable_path = './chromedriver.exe')
# driver.get(url)
# time.sleep(10)
with open('cookies.json', 'r', encoding='utf-8') as fd:
listCookies = json.loads(fd.read())
for cookie in listCookies:
cookies = {
'domain': cookie['domain'],
'httpOnly': cookie['httpOnly'],
'name':cookie['name'],
'path':'/',
'secure': cookie['secure'],
'value':cookie['value'],
}
print(cookies)
driver.add_cookie(cookies)
driver.get(url)
time.sleep(10)
exit()
driver.implicitly_wait(10) driver.implicitly_wait(10)
driver.get(url) driver.get(url)
@ -54,12 +76,18 @@ def manmanbuy(url):
driver.close() driver.close()
return response return response
def saveCookies(driver):
jsonCookies = json.dumps(driver.get_cookies())
with open('cookies.json', 'w', encoding='utf-8') as fd:
fd.write(jsonCookies)
fd.close()
def buy(): def buy():
jdurl = "https://item.jd.com/10047511027349.html" jdurl = "https://item.jd.com/10047511027349.html"
url = "https://tool.manmanbuy.com/HistoryLowest.aspx?url=" + jdurl url = "https://tool.manmanbuy.com/HistoryLowest.aspx?url=" + jdurl
# print(url) # print(url)
response = getsource(url) response = manmanbuy(url)
print(response) print(response)
if __name__ == "__main__": if __name__ == "__main__":

@ -36,4 +36,4 @@ USER_AGENT = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0' 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0'
] ]

Loading…
Cancel
Save