因业务原因,需要爬取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()
一个简单的python 爬取 soap数据
最新推荐文章于 2021-02-21 18:52:00 发布