欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3爬蟲帶上cookie的實(shí)例代碼

 更新時(shí)間:2020年07月28日 14:14:01   作者:yang  
在本篇文章里小編給各位分享的是一篇關(guān)于Python3爬蟲帶上cookie的實(shí)例代碼內(nèi)容,需要的朋友們可以學(xué)習(xí)下。

Cookie的英文原意是“點(diǎn)心”,它是在客戶端訪問Web服務(wù)器時(shí),服務(wù)器在客戶端硬盤上存放的信息,好像是服務(wù)器發(fā)送給客戶的“點(diǎn)心”。服務(wù)器可以根據(jù)Cookie來跟蹤客戶狀態(tài),這對(duì)于需要區(qū)別客戶的場合(如電子商務(wù))特別有用。

當(dāng)客戶端首次請(qǐng)求訪問服務(wù)器時(shí),服務(wù)器先在客戶端存放包含該客戶的相關(guān)信息的Cookie,以后客戶端每次請(qǐng)求訪問服務(wù)器時(shí),都會(huì)在HTTP請(qǐng)求數(shù)據(jù)中包含Cookie,服務(wù)器解析HTTP請(qǐng)求中的Cookie,就能由此獲得關(guān)于客戶的相關(guān)信息。

下面我們就來看一下python3爬蟲帶上cookie的方法:

1、直接將Cookie寫在header頭部

# coding:utf-8
import requests
from bs4 import BeautifulSoup
cookie = '''cisession=19dfd70a27ec0eecf1fe3fc2e48b7f91c7c83c60;CNZZDATA1000201968=181584
6425-1478580135-https%253A%252F%252Fwww.baidu.com%252F%7C1483922031;Hm_lvt_f805f7762a9a2
37a0deac37015e9f6d9=1482722012,1483926313;Hm_lpvt_f805f7762a9a237a0deac37015e9f6d9=14839
26368'''
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Geck
o) Chrome/53.0.2785.143 Safari/537.36',
'Connection': 'keep-alive',
'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Cookie': cookie}
url = 'http://www.dbjr.com.cn/article/191947.htm'
wbdata = requests.get(url,headers=header).text
soup = BeautifulSoup(wbdata,'lxml')
print(soup)

2、使用requests插入Cookie

# coding:utf-8
import requests
from bs4 import BeautifulSoup
cookie = {
"cisession":"19dfd70a27ec0eecf1fe3fc2e48b7f91c7c83c60",
"CNZZDATA100020196":"1815846425-1478580135-https%253A%252F%252Fwww.baidu.com%252F%7C1483
922031",
"Hm_lvt_f805f7762a9a237a0deac37015e9f6d9":"1482722012,1483926313",
"Hm_lpvt_f805f7762a9a237a0deac37015e9f6d9":"1483926368"
}
url = 'http://www.dbjr.com.cn/article/191947.htm'
wbdata = requests.get(url,cookies=cookie).text
soup = BeautifulSoup(wbdata,'lxml')
print(soup)

實(shí)例擴(kuò)展:

使用cookie登錄哈工大ACM站點(diǎn)

獲取站點(diǎn)登錄地址

http://acm.hit.edu.cn/hoj/system/login

查看要傳送的post數(shù)據(jù)
user和password

Code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = 'pi'
__email__ = 'pipisorry@126.com'

"""
import urllib.request, urllib.parse, urllib.error
import http.cookiejar

LOGIN_URL = 'http://acm.hit.edu.cn/hoj/system/login'
values = {'user': '******', 'password': '******'} # , 'submit' : 'Login'
postdata = urllib.parse.urlencode(values).encode()
user_agent = r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36'
headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}

cookie_filename = 'cookie.txt'
cookie = http.cookiejar.MozillaCookieJar(cookie_filename)
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)

request = urllib.request.Request(LOGIN_URL, postdata, headers)
try:
  response = opener.open(request)
  page = response.read().decode()
  # print(page)
except urllib.error.URLError as e:
  print(e.code, ':', e.reason)

cookie.save(ignore_discard=True, ignore_expires=True) # 保存cookie到cookie.txt中
print(cookie)
for item in cookie:
  print('Name = ' + item.name)
  print('Value = ' + item.value)

get_url = 'http://acm.hit.edu.cn/hoj/problem/solution/?problem=1' # 利用cookie請(qǐng)求訪問還有一個(gè)網(wǎng)址
get_request = urllib.request.Request(get_url, headers=headers)
get_response = opener.open(get_request)
print(get_response.read().decode())
# print('You have not solved this problem' in get_response.read().decode())

到此這篇關(guān)于Python3爬蟲帶上cookie的實(shí)例代碼的文章就介紹到這了,更多相關(guān)Python3爬蟲如何帶上cookie內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python Allure庫的使用示例教程

    Python Allure庫的使用示例教程

    Python Allure庫是一個(gè)實(shí)用可靠的測試報(bào)告框架,它幾乎可以與Python的其他庫和框架無縫集成,利用Python Allure庫,可以輕松生成易于閱讀的測試報(bào)告,讓測試變得更加簡單便捷,本文主要介紹Python Allure庫的使用,感興趣的朋友一起看看吧
    2023-12-12
  • 詳解Python中字符串前“b”,“r”,“u”,“f”的作用

    詳解Python中字符串前“b”,“r”,“u”,“f”的作用

    這篇文章主要介紹了Python中字符串前“b”,“r”,“u”,“f”的作用,感興趣的朋友跟隨小編一起看看吧
    2019-12-12
  • Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程

    Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程

    這篇文章主要介紹了Python數(shù)據(jù)處理篇之Sympy系列(五)---解方程,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • python使用PyGame模塊播放聲音的方法

    python使用PyGame模塊播放聲音的方法

    這篇文章主要介紹了python使用PyGame模塊播放聲音的方法,實(shí)例分析了PyGame模塊的使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼

    Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼

    這篇文章主要介紹了Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • postman傳遞當(dāng)前時(shí)間戳實(shí)例詳解

    postman傳遞當(dāng)前時(shí)間戳實(shí)例詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于postman傳遞當(dāng)前時(shí)間戳知識(shí)點(diǎn)相關(guān)內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2019-09-09
  • python生成ppt的方法

    python生成ppt的方法

    這篇文章主要為大家詳細(xì)介紹了python生成ppt的方法,通過python生成ppt文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • CentOS6.9 Python環(huán)境配置(python2.7、pip、virtualenv)

    CentOS6.9 Python環(huán)境配置(python2.7、pip、virtualenv)

    這篇文章主要介紹了CentOS6.9 Python環(huán)境配置(python2.7、pip、virtualenv)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-05-05
  • 200行python代碼實(shí)現(xiàn)貪吃蛇游戲

    200行python代碼實(shí)現(xiàn)貪吃蛇游戲

    這篇文章主要為大家詳細(xì)介紹了200行python代碼實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Python如何抓取天貓商品詳細(xì)信息及交易記錄

    Python如何抓取天貓商品詳細(xì)信息及交易記錄

    這篇文章主要為大家詳細(xì)介紹了Python如何抓取天貓商品詳細(xì)信息及交易記錄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02

最新評(píng)論