用request爬虫采集网页数据并保存到文本文件Python代码
时间: 2024-06-08 20:05:46 浏览: 17
以下是一个简单的示例代码,演示如何使用 requests 库爬取网页并将其保存到文本文件中:
```
import requests
url = "https://www.example.com"
response = requests.get(url)
if response.status_code == 200:
with open("example.html", "w", encoding="utf-8") as f:
f.write(response.text)
print("网页内容已保存到 example.html 文件中!")
else:
print("网页无法访问,状态码为:", response.status_code)
```
这个示例代码中,我们首先定义了要爬取的网页 URL,然后使用 `requests.get()` 方法向该 URL 发送 GET 请求,获取网页内容。如果请求成功(即返回状态码为 200),就将网页内容写入一个名为 `example.html` 的文本文件中,最后输出提示信息。如果请求失败,则输出状态码,表示无法访问该网页。
你可以根据需要修改代码,以适应不同的网页爬取和数据保存需求。
相关问题
使用Python爬虫怎么将网页图片保存到本地
使用Python爬虫将网页图片保存到本地有很多方法,这里介绍两种比较常用的方法。
方法一:使用requests库下载图片并保存到本地
```python
import requests
import os
# 目标图片URL
url = 'https://www.example.com/image.jpg'
# 创建目标文件夹
if not os.path.exists('images'):
os.mkdir('images')
# 下载并保存图片
img_data = requests.get(url).content
img_path = os.path.join('images', 'image.jpg')
with open(img_path, 'wb') as f:
f.write(img_data)
print(f'Saved {img_path}')
```
这个程序使用 `requests` 库下载图片,并将图片内容保存到 `img_data` 变量中。然后,程序指定图片保存的路径,并使用 `open` 函数将图片内容写入文件中。最后,程序输出保存成功的消息并结束。
方法二:使用urllib库下载图片并保存到本地
```python
import urllib.request
import os
# 目标图片URL
url = 'https://www.example.com/image.jpg'
# 创建目标文件夹
if not os.path.exists('images'):
os.mkdir('images')
# 下载并保存图片
img_path = os.path.join('images', 'image.jpg')
urllib.request.urlretrieve(url, img_path)
print(f'Saved {img_path}')
```
这个程序使用 `urllib.request` 库下载图片,并将图片保存到指定路径中。程序使用 `urlretrieve` 函数下载图片,并指定保存路径和文件名。最后,程序输出保存成功的消息并结束。
以上两种方法都可以实现将网页图片保存到本地的功能。其中,第一种方法使用 `requests` 库,需要先使用 `requests.get` 函数获取图片的二进制数据,然后再将数据写入文件中。第二种方法使用 `urllib.request` 库,可以直接使用 `urlretrieve` 函数下载图片并保存到本地。
python爬虫天气数据request代码
以下是使用 requests 库爬取天气数据的 Python 代码示例:
```python
import requests
# 构造请求 URL
url = 'https://www.tianqi.com/air/'
# 发送 GET 请求
response = requests.get(url)
# 获取响应内容
html_content = response.content.decode('utf-8')
# 处理响应内容,提取需要的数据
# ...
# 关闭响应
response.close()
```
请注意,这只是一个简单的示例,具体的代码实现可能需要根据实际情况进行调整。另外,爬取网站数据时应遵守相关法律法规,不得进行恶意爬虫行为。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)