Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #60776

Re: strip away html tags from extracted links

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <joel.goldstick@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.014
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'python.': 0.02; 'sys': 0.07; 'cashiers': 0.09; 'lawrence': 0.09; 'req': 0.09; 'technician': 0.09; 'url:software': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'soup': 0.16; 'url:example': 0.16; 'urllib2,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'email addr:gmail.com&gt;': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'rid': 0.24; 'looks': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'task': 0.26; 'this:': 0.26; 'certain': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'officer': 0.29; 'message-id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'code': 0.31; 'getting': 0.31; "skip:' 10": 0.31; 'baker': 0.31; 'extract': 0.31; 'fri,': 0.33; 'skip:& 30': 0.33; 'subject:from': 0.34; 'common': 0.35; 'skip:u 20': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:o 20': 0.38; 'skip:& 10': 0.38; 'follows:': 0.38; 'nov': 0.38; 'pm,': 0.38; 'skip:& 20': 0.39; 'skip:p 20': 0.39; 'skip:\xc2 10': 0.60; 'gone': 0.61; 'skip:t 30': 0.61; 'here:': 0.62; 'more': 0.64; 'within': 0.65; 'close': 0.67; 'beautiful': 0.68; 'marketing': 0.70; '&lt;a': 0.84; 'pardon': 0.84; 'to:addr:yahoo.co.uk': 0.84; 'joel': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=D4Gt4/7SXGUIsAnaDAFqCNVRfsDOlqCgvUXZ76VSQrk=; b=JvSmTiffXYBFYdYQk3/5NKbRh6ghI6d4dDGgydJIoTuVL65BRuFDZyWIcG/RfP/eno Fj53NTpFc2UTxaxDb9uU7hYhvIU/eBrLrf6H+mxTdz8stUrllafJO6/IvrjojsklTuFx t8diRcuAWTryUNmqi/U/0JFJT1VKKZ3aK+C/XaZQF0EVFchdBgO4NiWwl2FdjeIp+teG g9PVZ0J7hgLp67c4begGaErtMNGZqGKrq/TK6I+/QqSiqElbX4byfkCnP1i9ZOybnmh+ qubt3J2LJ3WZ24S9/VyuU5eaMVrTb99gYw0wLlVF8dRaE8K9tqrrDWHrPpnRmBvIG1f4 bC5A==
MIME-Version 1.0
X-Received by 10.58.228.231 with SMTP id sl7mr1016850vec.49.1385747154871; Fri, 29 Nov 2013 09:45:54 -0800 (PST)
In-Reply-To <CAPM-O+yXHomAcHWOVVb4Kn98fc5EV5=EUGJYZq==Wb3-NttbZA@mail.gmail.com>
References <ab5d3c8b-401f-458d-9701-fa283936a6ff@googlegroups.com> <l7aj48$84p$1@ger.gmane.org> <CAPM-O+yXHomAcHWOVVb4Kn98fc5EV5=EUGJYZq==Wb3-NttbZA@mail.gmail.com>
Date Fri, 29 Nov 2013 12:45:54 -0500
Subject Re: strip away html tags from extracted links
From Joel Goldstick <joel.goldstick@gmail.com>
To Mark Lawrence <breamoreboy@yahoo.co.uk>
Content-Type multipart/alternative; boundary=047d7bd6b4b2ea2c8504ec5466f2
Cc "python-list@python.org" <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3405.1385747163.18130.python-list@python.org> (permalink)
Lines 199
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1385747164 news.xs4all.nl 15935 [2001:888:2000:d::a6]:53205
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:60776

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On Fri, Nov 29, 2013 at 12:44 PM, Joel Goldstick
<joel.goldstick@gmail.com>wrote:

>
>
>
> On Fri, Nov 29, 2013 at 12:33 PM, Mark Lawrence <breamoreboy@yahoo.co.uk>wrote:
>
>> On 29/11/2013 16:56, Max Cuban wrote:
>>
>>> I have the following code to extract certain links from a webpage:
>>>
>>> from bs4 import BeautifulSoup
>>> import urllib2, sys
>>> import re
>>>
>>> def tonaton():
>>>      site = "http://tonaton.com/en/job-vacancies-in-ghana"
>>>      hdr = {'User-Agent' : 'Mozilla/5.0'}
>>>      req = urllib2.Request(site, headers=hdr)
>>>      jobpass = urllib2.urlopen(req)
>>>      invalid_tag = ('h2')
>>>      soup = BeautifulSoup(jobpass)
>>>      print soup.find_all('h2')
>>>
>>> The links are contained in the 'h2' tags so I get the links as follows:
>>>
>>> <h2><a href="/en/cashiers-accra">cashiers </a></h2>
>>> <h2><a href="/en/cake-baker-accra">Cake baker</a></h2>
>>> <h2><a href="/en/automobile-technician-accra">Automobile
>>> Technician</a></h2>
>>> <h2><a href="/en/marketing-officer-accra-4">Marketing Officer</a></h2>
>>>
>>> But I'm interested in getting rid of all the 'h2' tags so that I have
>>> links only in this manner:
>>>
>>> <a href="/en/cashiers-accra">cashiers </a>
>>> <a href="/en/cake-baker-accra">Cake baker</a>
>>> <a href="/en/automobile-technician-accra">Automobile Technician</a>
>>> <a href="/en/marketing-officer-accra-4">Marketing Officer</a>
>>>
>>>
>>> This is more a beautiful soup question than python.  Have you gone
>>> through their tutorial.  Check here:
>>>
>>
> They have an example that looks close here:
> http://www.crummy.com/software/BeautifulSoup/bs4/doc/
>
> One common task is extracting all the URLs found within a page’s <a> tags:
>
> for link in soup.find_all('a'):
>     print(link.get('href'))
> # http://example.com/elsie
> # http://example.com/lacie
> # http://example.com/tillie
>
> In your case, you want the href values for the child of the h2 refences.
>
> So this might be close (untested)
>

Pardon my typo.  Try this:

>
> for link in soup.find_all('h2'):
>     print (link.a.get('href'))
> # http://example.com/elsie
> # http://example.com/lacie
> # http://example.com/tillie
>
>
>
>
>
>
> --
> Joel Goldstick
> http://joelgoldstick.com
>



-- 
Joel Goldstick
http://joelgoldstick.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

strip away html tags from extracted links Max Cuban <edzeame@gmail.com> - 2013-11-29 08:56 -0800
  Re: strip away html tags from extracted links Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-29 17:33 +0000
  Re: strip away html tags from extracted links Chris Angelico <rosuav@gmail.com> - 2013-11-30 04:41 +1100
  Re: strip away html tags from extracted links Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-29 12:44 -0500
  Re: strip away html tags from extracted links Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-29 12:45 -0500
  Re: strip away html tags from extracted links Gene Heskett <gheskett@shentel.net> - 2013-11-29 13:45 -0500

csiph-web