[英]Remove items from list not in 'speeches'?
url = 'http://www.millercenter.org/president/speeches'
conn = urllib2.urlopen(url)
html = conn.read()
miller_center_soup = BeautifulSoup(html)
links = miller_center_soup.find_all('a')
linklist = [tag.get('href') for tag in links if tag.get('href') is not None]
linklist = str(linklist)
end_of_links = [line for line in linklist if '/events/' in line]
print end_of_links
這是我的輸出的一小段(保存在Python列表中)。
['/events/2015/one-nation-under-god-how-corporate-america-invented-christian-america',
'/events/2015/a-conversation-with-bernie-sanders', '#reagan', '#gwbush', '#obama',
'#top', '/president/obama/speeches/speech-4427', president/obama/speeches/speech-4430', ...]
我要刪除列表中不包含speeches
所有項目。 我試過了filter()
並只是創建了另一個列表理解,但是還沒有奏效。 我不知道為什么end_of_links
變量不起作用-至少對我來說直觀。
li = ['/ events / 2015 / one-nation-under-god-how-corporate-america-invented-christian-america','/ events / 2015 / a-conversation-with-bernie-sanders','#reagan ','#gwbush','#obama','#top','/ president / obama / speeches / speech-4427','president / obama / speeches / speech-4430']
匯入
li = [如果re.search('speeches',x),則x表示li中的x
打印(li)
['/ president / obama / speechs / speech-4427','president / obama / speechs / speech-4430']
昆明理工保持確實包括“演講”的那些:
link_list = ['/events/2015/one-nation-under-god-how-corporate-america-invented-christian-america',
'/events/2015/a-conversation-with-bernie-sanders', '#reagan', '#gwbush', '#obama',
'#top', '/president/obama/speeches/speech-4427', 'president/obama/speeches/speech-4430']
speech_list = [_ for _ in link_list if 'speeches' in _]
這是我在Python2.7中的終端會話
>>> link_list = ['/events/2015/one-nation-under-god-how-corporate-america-invented-christian-america',
... '/events/2015/a-conversation-with-bernie-sanders', '#reagan', '#gwbush', '#obama',
... '#top', '/president/obama/speeches/speech-4427', 'president/obama/speeches/speech-4430']
>>> speech_list = [_ for _ in link_list if 'speeches' in _]
>>> speech_list
['/president/obama/speeches/speech-4427', 'president/obama/speeches/speech-4430']
>>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.