python 爬虫例子
时间: 2024-01-15 13:19:46 浏览: 79
以下是一些Python爬虫的例子:
1. 使用requests库进行简单的网页爬取:
```python
import requests
url = 'https://www.example.com'
response = requests.get(url)
print(response.text)
```
2. 使用BeautifulSoup库解析HTML页面:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title)
```
3. 使用Selenium库模拟浏览器行为:
```python
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.example.com')
print(driver.title)
driver.quit()
```
4. 使用Scrapy框架进行高级的网页爬取:
```python
import scrapy
class MySpider(scrapy.Spider):
name = 'example'
start_urls = ['https://www.example.com']
def parse(self, response):
# 解析网页内容
title = response.css('title::text').get()
print(title)
```
5. 使用API进行数据爬取:
```python
import requests
url = 'https://api.example.com/data'
response = requests.get(url)
data = response.json()
print(data)
```
这些例子只是爬虫的冰山一角,你可以根据自己的需求和兴趣进一步探索更多的爬虫技术和案例。
阅读全文