ifrom urllib2 import urlparse
‘’
captcha_id = urlparse.parse_qs(urlparse.urlparse(link).query, True)['id']
‘’
url=https://www.douban.com/misc/captcha?id=iY6w7im3vNcLwLRiC66OxNB5:en&size=s
1. urlparse.urlparse(url)
执行结果是这样的,分成了6个部分
ParseResult(scheme='https', netloc='www.douban.com', path='/misc/captcha',
params='', query='id=iY6w7im3vNcLwLRiC66OxNB5:en&size=s', fragment='')
2.获取query数值
urlparse.urlparse(link).query
结果是'id=iY6w7im3vNcLwLRiC66OxNB5:en&size=s'
3.格式化成字典
urlparse.parse_qs('id=iY6w7im3vNcLwLRiC66OxNB5:en&size=s')
j结果是{'id': ['iY6w7im3vNcLwLRiC66OxNB5:en'], 'size': ['s']}
4.获取字段id值
urlparse.parse_qs('id=iY6w7im3vNcLwLRiC66OxNB5:en&size=s')['id']
#['iY6w7im3vNcLwLRiC66OxNB5:en']