ITPub博客

首页 > 大数据 > 数据分析 > 使用requests库来发送HTTP请求

使用requests库来发送HTTP请求

原创 数据分析 作者:金木大大大 时间:2023-11-08 10:26:16 0 删除 编辑

 首先,你需要安装Python的requests和BeautifulSoup库。然后,你可以使用requests库来发送HTTP请求,并用BeautifulSoup库来解析HTML文档。


```python

import requests

from bs4 import BeautifulSoup

```


`requests.get('http://www.91hfdm.com')` 这行代码会向 'http://www.91hfdm.com' 发送一个GET请求,然后返回响应。


`response.text` 这行代码会返回响应的文本内容。


```python

soup = BeautifulSoup(response.text, 'lxml')

```


`BeautifulSoup(response.text, 'lxml')` 这行代码会将响应的文本内容解析为BeautifulSoup对象。


`soup.find_all('a')` 这行代码会找到所有的`<a>`标签。


```python

for link in soup.find_all('a'):

    print(link.get('href'))

```


`for link in soup.find_all('a')` 这行代码会遍历所有的`<a>`标签。


`print(link.get('href'))` 这行代码会打印出每个`<a>`标签的`href`属性。


```python

# 使用代理

proxy_host = 'jshk.com.cn'



proxy = {

    'http': 'http://' + proxy_host + ':' + str(proxy_port),

    'https': 'http://' + proxy_host + ':' + str(proxy_port)

}


response = requests.get('http://www.91hfdm.com', proxies=proxy)

```


`proxy = {'http': 'http://' + proxy_host + ':' + str(proxy_port), 'https': 'http://' + proxy_host + ':' + str(proxy_port)}` 这行代码会创建一个字典,表示HTTP代理。


`response = requests.get('http://www.91hfdm.com', proxies=proxy)` 这行代码会使用HTTP代理发送GET请求。


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70032566/viewspace-2993422/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: 如何导入rvest包
请登录后发表评论 登录
全部评论

注册时间:2023-10-12

  • 博文量
    249
  • 访问量
    157736