python经典语录
时间: 2023-11-15 21:02:56 浏览: 143
python语句
Python经典语录是指一些经典的Python编程语句或者代码段,这些语录可以帮助Python程序员更好地理解和掌握Python编程语言。下面是一个获取鲁迅经典语录并保存到Excel文件的Python代码示例:
```
import requests
from lxml import etree
import pandas as pd
import time
item_list = []
# 爬取数据
for i in range(10):
url = 'http://www.shuoshuodaitupian.com/writer/128_{}'.format(i + 1)
result = requests.get(url).text
# 解析数据
html = etree.HTML(result)
div_list = html.xpath('//div[@class="item statistic_item"]')
div_list = div_list[1:-1]
for div in div_list:
item = {}
item['content'] = div.xpath('./a/text()')[0]
item['source'] = div.xpath('./div[@class="author_zuopin"]/text()')[0]
item['score'] = div.xpath('.//a[@class="infobox zan like "]/span/text()')[0]
item_list.append(item)
print("正在爬取第{}页".format(i + 1))
time.sleep(0.1)
# 保存数据
df = pd.DataFrame(item_list)
df.to_csv('鲁迅经典语录.csv', encoding='utf_8_sig')
```
阅读全文