一个简单的python 爬取 soap数据

因业务原因,需要爬取soap数据,自己网上摸索了一段时间,新手小白,求指导


import requests
import json
from bs4 import BeautifulSoup as bs

#定义soap数据的请求文
saop_body = ['''
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <q0:findBikeSites></q0:findBikeSites></soapenv:Body> </soapenv:Envelope>
''',
'''
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <q0:findAbnormalSites></q0:findAbnormalSites></soapenv:Body> </soapenv:Envelope>
''']
#定义头信息
request_headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.92 Safari/537.36',
    'Content-Type': 'text/xml; charset=utf-8',
    }
#定义请求网址
urls = ["http://www.xazxc.com/service/IBikeSitesService?wsdl/findBikeSites","http://www.xazxc.com/service/IAbnormalSiteService?wsdl/findAbnormalSites"]
data_all = []
for i in saop_body:
    for j in urls:
        try:
            response = requests.post(j, data=i,headers=request_headers)
            data = response.text
            data = bs(data,"lxml")
            conns = data.find("ns1:out").get_text()
            conns = json.loads(conns)
            for conn in conns:
                name = conn["sitename"]
                address = conn["location"]
                lat = conn["latitude"]
                lng = conn["longitude"]
                str = name + ',' + address + ',' + lat + ',' + lng
                print(str)
                data_all.append(str)
        except Exception as e:
            pass
doc  = open("axc.csv","a+")
for connect in data_all:
    doc.write(connect+'\n')
doc.close()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值