import requests
import pymysql
from lxml import etree
import pandas as pd
# UA伪装
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}
# url连接
url = 'https://www.9game.cn/czymf/2174371.html'
response = requests.get(url, headers=headers)
# print(response)
pege = response.text
# print(pege)
# 解析数据
tree = etree.HTML(pege)
list = tree.xpath('//p[@align="center"]')
# print(list)
c = []
# for i in list:
# title = i.xpath('.//text()')
# c.append(title)
# print(c)
# df = pd.DataFrame(c)
# df.to_excel('c.xlsx')
for i in list:
title = i.xpath('.//text()')
print(title)
with open('食谱大全.txt','w',encoding='utf8') as f:
for l in title:
f.write(l+"\n")
# 存储数据
# # 持久化保存数据
# with open('创造与魔法.html','w',encoding='utf8') as fp:
# fp.write(pege)
# print("爬取数据结束")