小工具知识网

当前位置: 首页 > 行业知识 > 爬虫与DataFrame对象小小结合

行业知识

爬虫与DataFrame对象小小结合

2026-11-14 17:04:17 来源:互联网转载
import pandas as pdimport requestsfrom lxml import etree#数据请求url="https://www.maigoo.com/brand/list_1715.html"headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.5735.289 Safari/537.36'}#数据响应res=requests.get(url,headers=headers)tree = etree.HTML(res.text)#数据解析title=tree.xpath('.//p[@]/a/text()')company=tree.xpath('.//p[@]//span//text()')company=" ".join(company).replace('(', '').replace(')', '')#获取数据,进行简单的处理,转成列表类型暂存数据。company=company.split('  ')content=tree.xpath('.//p[@]/text()')adress=tree.xpath('.//p[@]/a/@href')picture=tree.xpath('.//p[@]//img/@src')#数据保存for i in title,content,adress,picture:    data = pd.DataFrame([title,company,content,adress,picture],index=['title','company','content','adress','picture'])print(data.T)

---如���侵权,请即使联系。谢谢~

上一篇:兰州哪里卖铁路服务器?

下一篇:高效工时管理利器,智能软件助你轻松记录与管理工时!