使用 Python 实现模拟登录和爬取 HTTPS 网站的方法

2023-04-17 00:00:00 方法 登录 模拟

Python 实现模拟登录和爬取 HTTPS 网站的方法:

  1. 模拟登录:

示例代码:

import requests

login_url = 'https://example.com/login'
username = 'your_username'
password = 'your_password'

# 构造登录请求表单数据
login_data = {
    'username': username,
    'password': password
}

# 发送 POST 请求登录
response = requests.post(url=login_url, data=login_data)

# 检查是否登录成功
if response.status_code == 200:
    print('登录成功')
else:
    print('登录失败')
  1. 爬取 HTTPS 网站:

示例代码:

import requests

url = 'https://pidancode.com'

# 发送 GET 请求获取页面内容
response = requests.get(url)

# 打印页面内容
print(response.content.decode('utf-8'))

若需要爬取 HTTPS 网站,只需要确保在请求时将url 前缀改为 https,其余与 HTTP 爬取相同。

相关文章