python爬出的数据提取
response = requests.post(url=url, params=data, headers=headers)
print(response.text)
这是数据,作为一行显示的,为了方便我换行了
才接触编程
把中文及日期(rq)提取出来并且输出至excel
要啥?
1 完成 姓名 111111111111X None None None 种类 1 2002-01-01 品牌 A202105069 地址
2 完成 姓名 111111111111X 111111111111X None None 种类 2 2021-06-29 品牌 B202106083 地点
3 完成 姓名 1111111111111111X None None None 种类 3 2021-10-15 品牌 A202107142 地点
方便的话把这个测试文件数据发一下,可以私信到我邮箱。
举个例子:
import re
with open('test.txt', 'r') as f:
str1 = f.readlines()
print(str1)
pattern = r'(\d{4}-\d{1,2}-\d{1,2})'
pattern = re.compile(pattern)
result = pattern.findall(str1[0])
print(result)
所谓的中文是什么。“完成”之类?
把你的链接URL发出来可以吗?
xml解析自己查查看
参照下面代码修改一下
取值:vin_value = first_child.getElementsByTagName('cell')[0]..firstChild.data
readXmlToExcel.py
import xml.dom.minidom #导入处理xml文件的模块
import pandas as pd
import xlwt
try:
vin_list = []
LicensePlateNo_list = []
LicensePlateType_list = []
LicensePlateType_list = []
EngineNo_list = []
PMVehicleType_list = []
PMUserNature_list = []
IneffectualDate_list = []
#使用minidom解析器打卡xml文档
dom = xml.dom.minidom.parse("body.xml")
#得到文档元素对象
Packet = dom.documentElement #用于得到dom对象的文档元素,并把获得的对象给root
if Packet.hasAttribute("shelf"):
print('Root element: %s' % (Packet.getAttribute("type")))
#获得标签为Body的多组标签
child_tag = Packet.getElementsByTagName("Body")
first_child = child_tag[0]
#车架号
vin_value = first_child.getElementsByTagName('cell')[0].childNodes[0].data #获得元素属性对应的值
print(vin_value)
vin_list.append(vin_value)
#号牌号码
LicensePlateNo = first_child.getElementsByTagName('LicensePlateNo')[0].firstChild.data
LicensePlateNo_list.append(LicensePlateNo)
#号牌种类代码
LicensePlateType = first_child.getElementsByTagName('LicensePlateType')[0].firstChild.data
LicensePlateType_list.append(LicensePlateType)
#发动机hao
EngineNo = first_child.getElementsByTagName('EngineNo')[0].firstChild.data
EngineNo_list.append(EngineNo)
#交管车辆类型
PMVehicleType = first_child.getElementsByTagName('PMVehicleType')[0].firstChild.data
PMVehicleType_list.append(PMVehicleType)
#车辆使用性质代码
PMUserNature = first_child.getElementsByTagName('PMUserNature')[0].firstChild.data
PMUserNature_list.append(PMVehicleType)
#校验有效日期
IneffectualDate = first_child.getElementsByTagName('IneffectualDate')[0].firstChild.data
IneffectualDate_list.append(IneffectualDate)
#将列表存储为字典
all_dict = {
'VIN':vin_list,
'LicensePlateNo':LicensePlateNo_list,
'LicensePlateType': LicensePlateType_list,
'EngineNo': EngineNo_list,
'PMVehicleType': PMVehicleType_list,
'PMUserNature': PMUserNature_list,
'IneffectualDate': IneffectualDate_list
}
df = pd.DataFrame(all_dict) #将字典转换为DataFrame
#将DataFrame数据写入excel表中
with pd.ExcelWriter('car_table.xls') as Writer:
df.to_excel(Writer,'Sheet1',index=False)
except xml.parsers.expat.ExpatError as e:
print(e)
根据返回的数据格式进行解析,
然后把解析后的数据记录下来写入excel
或者输出打印,再复制到excel