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; 'example': 0.22; 'import': 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; '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; '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=urNjatjcyt1segPd7pqM1qBNCD1oa0R9HK7rnnr1Hrk=; b=oiJg1+qWhIEliuoEOrMJABV+4AU450cvXzuh2Iq9d/eg3hCTPacLEUcbYD46+kH3fq jCBDbUL/a34/rW9iZNpD69CMctfX/ELaKFBO4zjh7Db9tX6t5PfKee3I+jaenkNARCQi ldzgCrrozVj7VM3al9iBcockYkBl8a5Pu1JZ8dwlCod8qh8N8lm5KX5uTAXzzyZFKlNh vfdI3AoZwrDrcAEgszMD8FsJiYHlgU97FkO1FkG+ls3taijjx1/f60cP3mM12QtpwVEq R96hgn4oqD2tMxuNZ9rNip5evHvfCpyEQqVBLCO+a8zacp0c7cZIfZ9rS+CYev5mGiVR 98/g== MIME-Version: 1.0 X-Received: by 10.58.107.4 with SMTP id gy4mr2134863veb.39.1385747053610; Fri, 29 Nov 2013 09:44:13 -0800 (PST) In-Reply-To: References: Date: Fri, 29 Nov 2013 12:44:13 -0500 Subject: Re: strip away html tags from extracted links From: Joel Goldstick To: Mark Lawrence Content-Type: multipart/alternative; boundary=089e0122f348e10e6004ec5460b8 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: 158 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1385747062 news.xs4all.nl 15920 [2001:888:2000:d::a6]:51211 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:60775 --089e0122f348e10e6004ec5460b8 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Fri, Nov 29, 2013 at 12:33 PM, Mark Lawrence wr= ote: > 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) for link in soup.find_all('a'): print (link.a.get('href')) # http://example.com/elsie # http://example.com/lacie # http://example.com/tillie --=20 Joel Goldstick http://joelgoldstick.com --089e0122f348e10e6004ec5460b8 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable



On Fri, Nov 29, 2013 at 12:33 PM, Mark Lawrence &= lt;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():
=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: http://www.crummy.co= m/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
# http://example.com/tillie

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

So this might be close (untested)
<= br>for link in soup.find_all('a'):
=C2=A0 =C2=A0 print (link.a.g= et('href'))
# http://exampl= e.com/elsie
# http://example.com/lacie
# http://example.com/tillie


=C2=A0


--=
--089e0122f348e10e6004ec5460b8--