帮我用python写一个简单的爬虫
时间: 2023-02-19 13:47:17 浏览: 59
好的,以下是一个简单的 Python 爬虫代码:
```
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com"
page = requests.get(url)
soup = BeautifulSoup(page.content, "html.parser")
print(soup.prettify())
```
这段代码使用 `requests` 库发送一个 GET 请求获取网页内容,然后使用 `BeautifulSoup` 库解析 HTML 内容,最后使用 `prettify()` 方法格式化输出。
请注意,上面的代码仅供参考,具体实现可能会有所不同,需要根据你的需求进行相应的修改。
相关问题
帮我用Python写一个简单的爬虫
很高兴为您服务!我可以为您提供一个简单的Python爬虫示例:
import requests
url = 'http://example.com'
response = requests.get(url)
if response.status_code == 200:
print(response.text)
帮我用python写一个爬虫案例
当然可以帮您写一个简单的Python爬虫案例。下面是一个示例,演示如何使用Python的requests和BeautifulSoup库来爬取网页内容:
```python
import requests
from bs4 import BeautifulSoup
# 发送HTTP请求获取网页内容
url = "https://example.com" # 替换为您要爬取的网页URL
response = requests.get(url)
html_content = response.text
# 使用BeautifulSoup解析网页内容
soup = BeautifulSoup(html_content, 'html.parser')
# 提取需要的数据
# 以下是一个示例,提取所有标题标签的内容
titles = soup.find_all('h1')
for title in titles:
print(title.text)
# 可以根据需要提取其他信息,如链接、图片等等
```
请注意,这只是一个简单的示例,实际的爬虫可能需要更复杂的逻辑和处理。另外,请确保遵守网站的爬虫规则,并尊重网站的隐私政策。