爬取豌豆荚软件排行榜1000条输出为txt
时间: 2023-06-23 12:09:53 浏览: 59
以下是使用Python爬取豌豆荚软件排行榜1000条并输出为txt的代码:
```python
import requests
from bs4 import BeautifulSoup
url = 'https://www.wandoujia.com/top/app'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
app_list = soup.find_all('li', class_='card')
with open('wandoujia_app_ranking.txt', 'w', encoding='utf-8') as f:
for index, app in enumerate(app_list):
rank = index + 1
name = app.find('a', class_='name').text.strip()
category = app.find('span', class_='cat-box').text.strip()
download_count = app.find('span', class_='download-count').text.strip()
f.write(f'{rank}. {name} ({category}) - {download_count}\n')
```
运行上述代码后,会在当前文件夹生成一个名为 `wandoujia_app_ranking.txt` 的文件,其中包含了豌豆荚软件排行榜的前1000名应用的名称、类别和下载量信息。
需要注意的是,爬虫的行为可能会对网站造成一定的访问压力,因此在使用爬虫时需要遵守网站的相关规定,并确保不会对网站造成过大的负担。