存入excel表格

import requests
import xlwt
from lxml import etree
if __name__=="__main__":
wb = xlwt.Workbook()
ws = wb.add_sheet('网站')
url = 'https://www.hao123.com/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.54'
}
response = requests.get(url=url,headers=headers).text
tree = etree.HTML(response)
li_list = tree.xpath('//*[@id="userCommonSites"]/ul/li')
ws.write(0,1,'网站名称')
ws.write(0,2,'网站链接')
i = 0
for li in li_list:
wz = li.xpath('./div/a/text()')[0]
lj = li.xpath('./div/a/@href')[0]
print(wz+lj)
ws.write(i+1,0,i+1)
ws.write(i+1,1,wz)
ws.write(i+1,2,lj)
i += 1
wb.save('网站网址.xls')
上一篇:Postman被低估的功能 — 自动化接口测试


下一篇:python | P站壁纸爬取