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: 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>': 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; '<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: References: Date: Fri, 29 Nov 2013 12:45:54 -0500 Subject: Re: strip away html tags from extracted links From: Joel Goldstick To: Mark Lawrence Content-Type: multipart/alternative; boundary=047d7bd6b4b2ea2c8504ec5466f2 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --047d7bd6b4b2ea2c8504ec5466f2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Fri, Nov 29, 2013 at 12:44 PM, Joel Goldstick wrote: > > > > On Fri, Nov 29, 2013 at 12:33 PM, Mark Lawrence = 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 =3D "http://tonaton.com/en/job-vacancies-in-ghana" >>> hdr =3D {'User-Agent' : 'Mozilla/5.0'} >>> req =3D urllib2.Request(site, headers=3Dhdr) >>> jobpass =3D urllib2.urlopen(req) >>> invalid_tag =3D ('h2') >>> soup =3D BeautifulSoup(jobpass) >>> print soup.find_all('h2') >>> >>> The links are contained in the 'h2' tags so I get the links as follows: >>> >>>

cashiers

>>>

Cake baker

>>>

Automobile >>> Technician

>>>

Marketing Officer >>> >>> But I'm interested in getting rid of all the 'h2' tags so that I have >>> links only in this manner: >>> >>> cashiers >>> Cake baker >>> Automobile Technician >>> Marketing Officer >>> >>> >>> 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=E2=80=99s = 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 > --=20 Joel Goldstick http://joelgoldstick.com --047d7bd6b4b2ea2c8504ec5466f2 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



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



On Fri, Nov 29, 20= 13 at 12:33 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
On 29/11/2013 16:56, Max Cuban wrote:
<= div> I have the following code to extract certain links from a webpage:

from bs4 import BeautifulSoup
import urllib2, sys
import re

def tonaton():
=C2=A0 =C2=A0 =C2=A0site =3D "http://tonaton.com/en/job-vacanci= es-in-ghana"
=C2=A0 =C2=A0 =C2=A0hdr =3D {'User-Agent' : 'Mozilla/5.0'}<= br> =C2=A0 =C2=A0 =C2=A0req =3D urllib2.Request(site, headers=3Dhdr)
=C2=A0 =C2=A0 =C2=A0jobpass =3D urllib2.urlopen(req)
=C2=A0 =C2=A0 =C2=A0invalid_tag =3D ('h2')
=C2=A0 =C2=A0 =C2=A0soup =3D BeautifulSoup(jobpass)
=C2=A0 =C2=A0 =C2=A0print soup.find_all('h2')

The links are contained in the 'h2' tags so I get the links as foll= ows:

<h2><a href=3D"/en/cashiers-accra">cashiers &l= t;/a></h2>
<h2><a href=3D"/en/cake-baker-accra">Cake bake= r</a></h2>
<h2><a href=3D"/en/automobile-technician-accra"&g= t;Automobile Technician</a></h2>
<h2><a href=3D"/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=3D"/en/cashiers-accra">cashiers </a> <a href=3D"/en/cake-baker-accra">Cake baker</a>= ;
<a href=3D"/en/automobile-technician-accra">Automobi= le Technician</a>
<a href=3D"/en/marketing-officer-accra-4">Marketing = Officer</a>


This is more a beautiful soup question than python.=C2=A0 Have you gone thr= ough their tutorial.=C2=A0 Check here:

They have an example that looks close here: ht= tp://www.crummy.com/software/BeautifulSoup/bs4/doc/

One common task is extracting all the URLs found within a page=E2=80=99= s <a> tags:

for link in soup.find_all('a'):
=C2=A0 = =C2=A0 print(link.get('href'))
# http://example.com/elsie
# http://example.com= /lacie
# htt= p://example.com/tillie

In your = case, you want the href values for the child of the h2 refences.

So this might be close (untested)
<= /div>

Pardon my typo.=C2=A0 Try= this:

= for link in soup.find_all('h2'):
=C2=A0 =C2=A0 print (link.a.get= ('href'))
# http://example.com/elsie
# http://example.com= /lacie
# htt= p://example.com/tillie


=C2=A0


--



--
J= oel Goldstick
http://joelgoldstick.com
--047d7bd6b4b2ea2c8504ec5466f2--