Parsing HTML with Beautiful Soup to get link after href
<p>This is a result of a <code>find_all('a')</code> (it's very long):</p>

<pre><code>&lt;/a&gt;, &lt;a class="btn text-default text-dark clear_filters pull-right group-ib" href="#" id="export_dialog_close" title="Cancel"&gt;&lt;span class="glyphicon glyphicon-remove"&gt;&lt;/span&gt;&lt;span&gt;Cancel&lt;/span&gt;&lt;/a&gt;, &lt;a href="/ais/index/port_moves/all/include_anchs:no/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:no/time_interval:1474948800.0_1475035200.00/per_page:50/portname:NOVOROSSIYSK/cb:6651/move_type:1/sort:SHIPNAME/direction:asc"&gt;Vessel Name&lt;/a&gt;, &lt;a href="/ais/index/port_moves/all/include_anchs:no/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:no/time_interval:1474948800.0_1475035200.00/per_page:50/portname:NOVOROSSIYSK/cb:6651/move_type:1/sort:TIMESTAMP_UTC/direction:asc"&gt;Timestamp&lt;/a&gt;, &lt;a href="/ais/index/port_moves/all/include_anchs:no/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:no/time_interval:1474948800.0_1475035200.00/per_page:50/portname:NOVOROSSIYSK/cb:6651/move_type:1/sort:PORT_NAME/direction:asc"&gt;Port&lt;/a&gt;, &lt;a href="/ais/index/port_moves/all/include_anchs:no/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:no/time_interval:1474948800.0_1475035200.00/per_page:50/portname:NOVOROSSIYSK/cb:6651/move_type:1/sort:MOVE_TYPE_NAME/direction:asc"&gt;Port Call type&lt;/a&gt;, &lt;a href="/ais/index/port_moves/all/include_anchs:no/ship_type:7/_:3525d580eade08cfdb72083b248185a9/in_transit:no/time_interval:1474948800.0_1475035200.00/per_page:50/portname:NOVOROSSIYSK/cb:6651/move_type:1/sort:ELAPSED/direction:asc"&gt;Time Elapsed&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:465271/imo:9495595/mmsi:373571000/vessel:SIDER%20LUCK/_:3525d580eade08cfdb72083b248185a9" title="View details for: SIDER LUCK"&gt;SIDER LUCK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/163/port_name:MILAZZO/_:3525d580eade08cfdb72083b248185a9" title="View details for: MILAZZO"&gt;MILAZZO&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:288753/imo:9389693/mmsi:249474000/vessel:OOCL%20ISTANBUL/_:3525d580eade08cfdb72083b248185a9" title="View details for: OOCL ISTANBUL"&gt;OOCL ISTANBUL&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/17436/port_name:AMBARLI/_:3525d580eade08cfdb72083b248185a9" title="View details for: AMBARLI"&gt;AMBARLI&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:754480/imo:9045613/mmsi:636014098/vessel:TK%20ROTTERDAM/_:3525d580eade08cfdb72083b248185a9" title="View details for: TK ROTTERDAM"&gt;TK ROTTERDAM&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/3504/port_name:DILISKELESI/_:3525d580eade08cfdb72083b248185a9" title="View details for: DILISKELESI"&gt;DILISKELESI&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:412277/imo:9039585/mmsi:353430000/vessel:SEA%20AEOLIS/_:3525d580eade08cfdb72083b248185a9" title="View details for: SEA AEOLIS"&gt;SEA AEOLIS&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/1/port_name:PIRAEUS/_:3525d580eade08cfdb72083b248185a9" title="View details for: PIRAEUS"&gt;PIRAEUS&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:346713/imo:7614599/mmsi:273327300/vessel:SOLIDAT/_:3525d580eade08cfdb72083b248185a9" title="View details for: SOLIDAT"&gt;SOLIDAT&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/883/port_name:SEVASTOPOL/_:3525d580eade08cfdb72083b248185a9" title="View details for: SEVASTOPOL"&gt;SEVASTOPOL&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:752974/imo:9195298/mmsi:636011072/vessel:OCEANPRINCESS/_:3525d580eade08cfdb72083b248185a9" title="View details for: OCEANPRINCESS"&gt;OCEANPRINCESS&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/21780/port_name:EREGLI/_:3525d580eade08cfdb72083b248185a9" title="View details for: EREGLI"&gt;EREGLI&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:201260/imo:9385075/mmsi:235102768/vessel:EMERALD%20BAY/_:3525d580eade08cfdb72083b248185a9" title="View details for: EMERALD BAY"&gt;EMERALD BAY&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:418956/imo:9102746/mmsi:356579000/vessel:MSC%20DON%20GIOVANNI/_:3525d580eade08cfdb72083b248185a9" title="View details for: MSC DON GIOVANNI"&gt;MSC DON GIOVANNI&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/67/port_name:CONSTANTA/_:3525d580eade08cfdb72083b248185a9" title="View details for: CONSTANTA"&gt;CONSTANTA&lt;/a&gt;, &lt;a href="/en/ais/details/ships/shipid:748395/imo:9460734/mmsi:622121422/vessel:WADI%20SAFAGA/_:3525d580eade08cfdb72083b248185a9" title="View details for: WADI SAFAGA"&gt;WADI SAFAGA&lt;/a&gt;, &lt;a href="/en/ais/details/ports/767/port_name:NOVOROSSIYSK/_:3525d580eade08cfdb72083b248185a9" title="View details for: NOVOROSSIYSK"&gt;NOVOROSSIYSK&lt;/a&gt;, &lt;a href="/en/ais/details/ports/997/port_name:DAMIETTA/_:3525d580eade08cfdb72083b248185a9" title="View details for: DAMIETTA"&gt;DAMIETTA&lt;/a&gt;
</code></pre>

<p>I want to pull out the strings that start with <code>/en/ais/details/ships/shipid:</code> such as:</p>

<pre><code>`&lt;a href="/en/ais/details/ships/shipid:465271/imo:9495595/mmsi:373571000/vessel:SIDER%20LUCK/_:3525d580eade08cfdb72083b248185a9"`
</code></pre>

<p>I was able to copy these examples (<a href="http://stackoverflow.com/questions/7732694/find-specific-link-w-beautifulsoup">Find specific link w/ beautifulsoup</a> or <a href="http://stackoverflow.com/questions/12611592/how-to-get-beautiful-soup-to-get-link-from-href-and-class">How to get Beautiful Soup to get link from href and class?</a>) but I would rather not use regex. </p>

<p>So far I have: </p>

<pre><code>for i in ase: #ase is where the html is sotred
        print(i.get('href')) #prints everysingle href. 
</code></pre>

<p>In short, my question is how do I only keep the <code>href</code>'s that have the string I'm interested in without using regex?</p>

<p><a href="http://stackoverflow.com/a/39901685/771848">@elethan's answer</a> is not the best one. It would <strong>find you all the links and only then filter them out</strong>. Why don't we just <em>get the links we needed straight with no extra filtering</em> - <code>BeautifulSoup</code> is very capable of that:</p>

<pre><code>prefix = "/en/ais/details/ships/shipid"
[a["href"] for a in soup("a", href=lambda x: x and x.startswith(prefix))]
</code></pre>

<p>Or, instead of a <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-function" rel="nofollow">function</a>, you can pass a <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#a-regular-expression" rel="nofollow">regular expression pattern</a> to check if a string "starts with" a desired sub-string:</p>

<pre><code>pattern = re.compile(r"^/en/ais/details/ships/shipid")
[a["href"] for a in soup("a", href=pattern)]
</code></pre>

<p><code>^</code> here denotes the beginning of a string.</p>

<p>Or, we can even use a <a class='doc-link' href="http://stackoverflow.com/documentation/beautifulsoup/1940/locating-elements/6340/using-css-selectors-to-locate-elements-in-beautifulsoup#t=201610061913079501684">CSS selector</a>:</p>

<pre><code>[a["href"] for a in soup.select('a[href^="/en/ais/details/ships/shipid"]')]
</code></pre>

<p><code>^=</code> is a "starts-with" selector.</p>

python html beautifulsoup