点击(此处)折叠或打开

  1. import requests
  2. # 登录后才能访问的网页
  3. url = 'http://www.baidu.com'
  4. # 浏览器登录后得到的cookie,也就是刚才复制的字符串
  5. cookie_str = r'key1=ualue1; key2=value2'
  6. # 把cookie字符串处理成字典,以便接下来使用python
  7. cookies = {}
  8. for line in cookie_str.split(';'):
  9.     key, value = line.split('=', 1)
  10.     cookies[key] = value

  11. # 设置请求头
  12. headers = {
  13.     'User-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36'}
  14. # 在发送get请求时带上请求头和cookies
  15. for i in range(10000):
  16.     requests.get(url, headers=headers, cookies=cookies).text


10-26 07:27