用python写一个爬取网页内容的爬虫
时间: 2024-06-09 20:12:21 浏览: 22
好的,下面是一个简单的Python爬虫示例代码:
```python
import requests
url = 'https://www.example.com'
response = requests.get(url)
if response.status_code == 200:
html_content = response.text
print(html_content)
else:
print('Error:', response.status_code)
```
这段代码使用requests库发送一个GET请求,获取网页内容,并打印出来。你可以将`url`换成你需要爬取的网页地址。
相关问题
用python写一个爬取图片的爬虫
很高兴为您解答!爬取图片的爬虫可以使用Python语言编写,可以通过调用urllib库中的urlretrieve()函数来获取图片,可以参考以下代码:
import urllib.request
url = 'http://example.com/image.png'
urllib.request.urlretrieve(url, 'image.png')
用python写一个爬取网页内容的爬虫,要求内容爬取成功后保存在txt文件里
好的,我可以帮你解决这个问题。以下是基本的代码:
```python
import requests
url = 'https://example.com' #将网址替换成你要爬取的网页地址
response = requests.get(url) #获取数据
with open('content.txt', 'w', encoding='utf-8') as f: #将获取到的数据存储在content.txt文件中
f.write(response.text)
```
在这个代码中,我们使用了requests库来获取网页的内容,并将其保存在一个txt文件中。你可以使用不同的库和文件格式来满足你的需求。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)