从 Python 中打开的 Safari 会话中提取 cookie
Extract cookies from open Safari session in Python
尝试按照本例中的示例进行操作:
Python urllib2 login to minecraft.net
我正在尝试从该网站获取文件,点击获取每个文件是一个繁琐的过程。我可以轻松地使用 urllib2 进行网络抓取,但是,当然,您必须登录到该网站才能获取数据。我尝试使用 Python 中的一种登录类型方法,但无法正常工作。
我的另一个选择是从 Safari 会话导出 cookie,这样我就可以 运行 我的网络抓取脚本来获取我需要的数据。
有谁知道如何将 cookie 从 Safari 会话导出到 Python 以访问网站数据?
这将允许您登录并下载文件,这使用 BeautifulSoup 和请求:
#!/usr/bin/python
from bs4 import BeautifulSoup
import requests
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
s = requests.Session()
soup = BeautifulSoup(s.get("https://minecraft.net/login").content)
authToken = soup.find('form', attrs={'id': 'loginForm'}).findNext('input')['value']
data = {
"username": "USERNAME",
"password": "PASSWORD",
"remember": "true",
"authenticityToken": authToken
}
s.post("https://minecraft.net/login", data=data)
soup = BeautifulSoup(s.get("https://minecraft.net//download").content)
download_file(soup.find("div", attrs={'id': 'platform-osx'}).findNext('p').findNext('')['href'])
尝试按照本例中的示例进行操作:
Python urllib2 login to minecraft.net
我正在尝试从该网站获取文件,点击获取每个文件是一个繁琐的过程。我可以轻松地使用 urllib2 进行网络抓取,但是,当然,您必须登录到该网站才能获取数据。我尝试使用 Python 中的一种登录类型方法,但无法正常工作。
我的另一个选择是从 Safari 会话导出 cookie,这样我就可以 运行 我的网络抓取脚本来获取我需要的数据。
有谁知道如何将 cookie 从 Safari 会话导出到 Python 以访问网站数据?
这将允许您登录并下载文件,这使用 BeautifulSoup 和请求:
#!/usr/bin/python
from bs4 import BeautifulSoup
import requests
def download_file(url):
local_filename = url.split('/')[-1]
# NOTE the stream=True parameter
r = requests.get(url, stream=True)
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk: # filter out keep-alive new chunks
f.write(chunk)
f.flush()
return local_filename
s = requests.Session()
soup = BeautifulSoup(s.get("https://minecraft.net/login").content)
authToken = soup.find('form', attrs={'id': 'loginForm'}).findNext('input')['value']
data = {
"username": "USERNAME",
"password": "PASSWORD",
"remember": "true",
"authenticityToken": authToken
}
s.post("https://minecraft.net/login", data=data)
soup = BeautifulSoup(s.get("https://minecraft.net//download").content)
download_file(soup.find("div", attrs={'id': 'platform-osx'}).findNext('p').findNext('')['href'])