python爬虫100例
时间: 2023-08-25 22:13:02 浏览: 139
python爬虫实例
3星 · 编辑精心推荐
以下是一些Python爬虫的例子:
1. 爬取网页的文本信息
```python
import requests
url = 'http://www.example.com'
response = requests.get(url)
if response.status_code == 200:
print(response.text)
```
2. 爬取网页的图片
```python
import requests
url = 'http://www.example.com/image.jpg'
response = requests.get(url)
if response.status_code == 200:
with open('image.jpg', 'wb') as f:
f.write(response.content)
```
3. 爬取网页中的链接
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.example.com'
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))
```
4. 爬取网页中的表格数据
```python
import requests
from bs4 import BeautifulSoup
url = 'http://www.example.com'
response = requests.get(url)
if response.status_code == 200:
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table')
for row in table.find_all('tr'):
for cell in row.find_all('td'):
print(cell.text)
```
5. 爬取网页中的RSS feeds
```python
import feedparser
url = 'http://www.example.com/rss.xml'
feed = feedparser.parse(url)
for entry in feed.entries:
print(entry.title)
print(entry.link)
```
以上是一些Python爬虫的例子,希望对你有帮助。
阅读全文