百度热搜链接 本章要点: pandas存储excel
pandas的使用
pandas的使用方法 安装pandas库:pin3 install pandas
import pandas
baidu_data = []
baidu_data.append({
'title':value,
'url':value
})
baiduexcel = pandas.DataFrame(baidu_data)
baiduexcel.to_excel('百度热搜.xlsx',index=False)
百度热搜代码
百度热搜代码:
import requests,pandas
from lxml import html
url = 'https://top.baidu.com/board?tab=realtime'
res = requests.get(url).text
dom = html.etree.HTML(res)
datas = dom.xpath("//div[@style='margin-bottom:20px']/div/div[2]/a")
baidu_data = []
for i in datas:
title = i.xpath("div[@class='c-single-text-ellipsis']/text()")[0].strip()
url = i.xpath("@href")[0]
baidu_data.append({
'title':title,
'url':url
})
baiduexcel = pandas.DataFrame(baidu_data)
baiduexcel.to_excel('百度热搜.xlsx',index=False)
效果图:
|