私信  •  关注

vezunchik

vezunchik 最近创建的主题
vezunchik 最近回复了
5 年前
回复了 vezunchik 创建的主题 » python trim帮助变量(单括号和双括号)

检查这个片段。您可以将regexp应用于选择器:

>>> from scrapy import Selector
>>> txt = """<img src="//d30v2pzvrfyzpo.cloudfront.net/uk/images/restaurants/65115.gif" itemprop="image" alt="Gourmet Sushi" width="100" height="100" onerror="this.onerror = null;this.src = '//dy3erx8o0a6nh.cloudfront.net/images/serp/serp_default_logo.svg';">"""
>>> sel = Selector(text=txt)
>>> sel.re("src\s?=\s?['\"]([^'\"]+)['\"]")
[u'//d30v2pzvrfyzpo.cloudfront.net/uk/images/restaurants/65115.gif', u'//dy3erx8o0a6nh.cloudfront.net/images/serp/serp_default_logo.svg']

所以,然后你可以 [response.urljoin(url) for url in sel.re("src\s?=\s?['\"]([^'\"]+)['\"]")]