五十行python爬虫代码
时间: 2023-07-03 17:29:19 浏览: 47
以下是一个简单的 Python 爬虫代码,可以爬取指定网页的标题、正文和所有图片的地址,并保存到本地:
```python
import requests
from bs4 import BeautifulSoup
import os
# 指定要爬取的网页地址
url = "http://www.example.com"
# 发送请求获取网页内容
response = requests.get(url)
html = response.text
# 使用 BeautifulSoup 解析 HTML 代码
soup = BeautifulSoup(html, "html.parser")
# 获取网页标题
title = soup.title.string
# 获取正文内容
content = ""
for p in soup.find_all("p"):
content += p.get_text()
# 获取所有图片地址
image_urls = []
for img in soup.find_all("img"):
image_urls.append(img["src"])
# 创建保存文件夹
if not os.path.exists(title):
os.makedirs(title)
# 保存正文内容到文件
with open(os.path.join(title, "content.txt"), "w", encoding="utf-8") as f:
f.write(content)
# 保存所有图片到文件
for i, url in enumerate(image_urls):
response = requests.get(url)
with open(os.path.join(title, f"image_{i}.jpg"), "wb") as f:
f.write(response.content)
```
这个代码使用了 requests 库发送 HTTP 请求,使用 BeautifulSoup 库解析 HTML 代码,使用 os 库创建文件夹和文件,并使用了循环和条件语句等基本语法。这个代码只是一个简单的示例,实际的爬虫代码需要更复杂的处理逻辑和错误处理机制。此外,应该遵守网站的爬虫规则,以避免被封 IP 或其他惩罚。