python 中Request库的使用、信息标记、信息提取相关题目及解答
时间: 2023-09-20 22:08:13 浏览: 47
以下是几个与 Request 库使用、信息标记和信息提取相关的题目及解答:
1. 如何使用 Request 库发送一个 GET 请求?
解答:可以使用如下代码实现:
```python
import requests
url = "https://www.example.com"
response = requests.get(url)
print(response.text)
```
2. 如何使用 Request 库发送一个 POST 请求?
解答:可以使用如下代码实现:
```python
import requests
url = "https://www.example.com"
data = {"username": "user1", "password": "123456"}
response = requests.post(url, data=data)
print(response.text)
```
3. 如何在 Request 库中添加请求头信息?
解答:可以使用如下代码实现:
```python
import requests
url = "https://www.example.com"
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299"}
response = requests.get(url, headers=headers)
print(response.text)
```
4. 如何使用 BeautifulSoup 库解析 HTML 页面?
解答:可以使用如下代码实现:
```python
from bs4 import BeautifulSoup
import requests
url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
print(soup.title.string)
```
5. 如何使用正则表达式从文本中提取信息?
解答:可以使用如下代码实现:
```python
import re
text = "This is a test string 123."
result = re.findall(r"\d+", text)
print(result)
```
以上是几个与 Request 库使用、信息标记和信息提取相关的题目及解答。