使用beautifulSoup,我想获得与某些href相关联的字符串,其中包含“/genre/”。例如,我使用以下命令获得了以下href:
soup.find_all('a', href=True)
输出为:
<a href="/genre/Animation?ref_=tt_stry_gnr"> Animation</a>,
<a href="/genre/Adventure?ref_=tt_stry_gnr"> Adventure</a>,
<a href="/genre/Family?ref_=tt_stry_gnr"> Family</a>,
<a href="/title/tt0235917/parentalguide?ref_=tt_stry_pg#certification"> See all certifications</a>,
<a href="/title/tt0235917/parentalguide?ref_=tt_stry_pg" itemprop="url"> View content advisory</a>,
但是,我只想选择“流派”作为链接,并将输出作为:
Animation
Adventure
Family
我试过使用:
import re
imdb_page.find_all('a', {'href': re.compile(r'/genre/\d.*')})
但我得到一个空数组。有什么想法吗?你知道吗
你在正则表达式中有个错误,应该是
正则表达式解释
^
将模式锚定在字符串的开头,/genre/
匹配genre
.*
匹配任何内容在
/genre/\d.*
\d
匹配任何数字。也就是说,您正在尝试匹配/genre/
(如href="/genre/1qwert"
)后面的数字。你知道吗但是在输入字符串中,没有
href
遵循这种模式。你知道吗因此得到一个空字符串。
相关问题 更多 >
编程相关推荐