You have two steps one download features from WFS and second write a shapefile fortunately both a common operations and are documented in the OGR Cookbook.
So cutting and pasting gives this (You will need to adjust the field names ect):
import sys
try:
from osgeo import ogr, osr, gdal
except:
sys.exit('ERROR: cannot find GDAL/OGR modules')
# Set the driver (optional)
wfs_drv = ogr.GetDriverByName('WFS')
# Speeds up querying WFS capabilities for services with alot of layers
gdal.SetConfigOption('OGR_WFS_LOAD_MULTIPLE_LAYER_DEFN', 'NO')
# Set config for paging. Works on WFS 2.0 services and WFS 1.0 and 1.1 with some other services.
gdal.SetConfigOption('OGR_WFS_PAGING_ALLOWED', 'YES')
gdal.SetConfigOption('OGR_WFS_PAGE_SIZE', '10000')
# Open the webservice
url = 'http://example-service.com/wfs'
wfs_ds = wfs_drv.Open('WFS:' + url)
if not wfs_ds:
sys.exit('ERROR: can not open WFS datasource')
else:
pass
# iterate over available layers
for i in range(wfs_ds.GetLayerCount()):
layer = wfs_ds.GetLayerByIndex(i)
srs = layer.GetSpatialRef()
print 'Layer: %s, Features: %s, SR: %s...' % (layer.GetName(), layer.GetFeatureCount(), srs.ExportToWkt()[0:50])
# set up the shapefile driver
driver = ogr.GetDriverByName("ESRI Shapefile")
# create the data source
data_source = driver.CreateDataSource("volcanoes.shp")
# create the spatial reference, WGS84
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# create the layer
layer = data_source.CreateLayer("volcanoes", srs, ogr.wkbPoint)
# Add the fields we're interested in
field_name = ogr.FieldDefn("Name", ogr.OFTString)
field_name.SetWidth(24)
layer.CreateField(field_name)
field_region = ogr.FieldDefn("Region", ogr.OFTString)
field_region.SetWidth(24)
layer.CreateField(field_region)
layer.CreateField(ogr.FieldDefn("Latitude", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("Longitude", ogr.OFTReal))
layer.CreateField(ogr.FieldDefn("Elevation", ogr.OFTInteger))
# Process the text file and add the attributes and features to the shapefile
# iterate over features
feat = layer.GetNextFeature()
while feat is not None:
feat = layer.GetNextFeature()
# create the feature
feature = ogr.Feature(layer.GetLayerDefn())
# Set the attributes using the values from the delimited text file
feature.SetField("Name", feat['Name'])
feature.SetField("Region", feat['Region'])
feature.SetField("Latitude", feat['Latitude'])
feature.SetField("Longitude", feat['Longitude'])
feature.SetField("Elevation", feat['Elev'])
# create the WKT for the feature using Python string formatting
wkt = "POINT(%f %f)" % (float(feat['Longitude']) , float(feat['Latitude']))
# Create the point from the Well Known Txt
point = ogr.CreateGeometryFromWkt(wkt)
# Set the feature geometry using the point
feature.SetGeometry(point)
# Create the feature in the layer (shapefile)
layer.CreateFeature(feature)
# Dereference the feature
feature = None
feat = None
# Save and close the data source
data_source = None