利用Python模拟登录pastebin.com的实现方法

  import requests

  from lxml import etree

  # 登录 https://pastebin.com/

  login_url = "https://pastebin.com/login"

  username = "kuang123321"

  password = "xxxxxx"

  api_dev_key = "4f45a996aa78079d8f7d14f104c45893"

  session = requests.Session()

  form_data = {

  "submit_hidden": "submit_hidden",

  "user_name": username,

  "user_password": password,

  "submit": "Login"

  }

  headers = {"content-type": "application/x-www-form-urlencoded"}

  res = session.post(login_url, data=form_data, headers=headers)

  print(res.status_code, res.url)

  # 获取 api_dev_key

  api_url = "https://pastebin.com/api"

  text = session.get(api_url).content.decode("utf-8")

  html = etree.HTML(text)

  target_divs = html.xpath('//*[@id="content_left"]/div[9]/div/text()')

  if target_divs:

  api_dev_key = target_divs[0]

  print(api_dev_key)

  else:

  raise Exception("cannot find api_dev_key")