首页 > 编程语言 > 详细

Python Pyppeteer 获取今日头条cookies 美化cookies

时间:2020-08-31 23:25:33      阅读:279      评论:0      收藏:0      [点我收藏+]
原文:https://www.jianshu.com/p/611ed6b75d47

1
import asyncio 2 from pyppeteer import launch 3 4 async def get_cookie(page): # 获取页面cookie 5 cookies = await page.cookies() 6 from prettytable import PrettyTable 7 table = PrettyTable() 8 for cookie in cookies: 9 # cookie表头 10 table.field_names = list((cookie.keys())) 11 # cookie内容 12 table.add_row(list(cookie.values())) 13 print(table) 14 15 async def main(): 16 # headless参数设为False,则变成有头模式 17 browser = await launch( 18 # headless=False 19 ) 20 21 page = await browser.newPage() 22 23 # 设置页面视图大小 24 await page.setViewport(viewport={width:1280, height:800}) 25 26 # 是否启用JS,enabled设为False,则无渲染效果 27 await page.setJavaScriptEnabled(enabled=True) 28 29 await page.goto(https://www.toutiao.com/) 30 31 # 打印页面cookies 32 # print(await page.cookies()) 33 await get_cookie(page) 34 35 # 打印页面文本 36 # print(await page.content()) 37 38 # 打印当前页标题 39 # print(await page.title()) 40 41 # 抓取新闻标题 42 # title_elements = await page.xpath(‘//div[@class="title-box"]/a‘) 43 # for item in title_elements: 44 # # 获取文本 45 # title_str = await (await item.getProperty(‘textContent‘)).jsonValue() 46 # print(await item.getProperty(‘textContent‘)) 47 # # 获取链接 48 # title_link = await (await item.getProperty(‘href‘)).jsonValue() 49 # print(title_str) 50 # print(title_link) 51 52 # 关闭浏览器 53 await browser.close() 54 55 asyncio.get_event_loop().run_until_complete(main())

 

输出: 

1
+----------------+----------------------------------------------------------------------+------------------+------+-------------------+------+----------+--------+---------+ 2 | name | value | domain | path | expires | size | httpOnly | secure | session | 3 +----------------+----------------------------------------------------------------------+------------------+------+-------------------+------+----------+--------+---------+ 4 | tt_webid | 6867160506068534791 | www.toutiao.com | / | 1606661418.853257 | 27 | False | False | False | 5 | __tasessionId | hub45arla1598885422427 | www.toutiao.com | / | 1598887222 | 35 | False | False | False | 6 | s_v_web_id | verify_kein3hs8_2xZeMWVz_K6PW_4jew_9Rcx_6Ji9p8zzTYmD | www.toutiao.com | / | -1 | 62 | False | False | True | 7 | ttcid | 0b94696219ec4365840eb7cc4b4fdbff41 | www.toutiao.com | / | 1630421421 | 39 | False | False | False | 8 | MONITOR_WEB_ID | 6c51725b-9c9d-4d51-8b0c-0ab2d3e8144c | .www.toutiao.com | / | 1606661421.183564 | 50 | False | False | False | 9 | tt_webid | 6867160506068534791 | .toutiao.com | / | 1606690222.570409 | 27 | False | False | False | 10 | tt_scid | wNaT3C4gnA-ppQbZR3OBGm1R6YeTBdZK9tAlR4igMBM-oHsaal05o0y489QdnXSF55f6 | www.toutiao.com | / | 1630421421 | 75 | False | False | False | 11 | WEATHER_CITY | %E5%8C%97%E4%BA%AC | www.toutiao.com | / | 1607525422 | 30 | False | False | False | 12 | csrftoken | f87b6e3a2e21665288a254c9cf061f43 | www.toutiao.com | / | 1630335022.57038 | 41 | False | False | False | 13 +----------------+----------------------------------------------------------------------+------------------+------+-------------------+------+----------+--------+---------+

 

 

 

 

 技术分享图片

Python Pyppeteer 获取今日头条cookies 美化cookies

原文:https://www.cnblogs.com/mixintu/p/13593441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!