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


Groups > comp.lang.python > #52487

Re: Getting a value that follows string.find()

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'method.': 0.07; 'source.': 0.07; 'subject:Getting': 0.07; 'string': 0.09; 'assuming': 0.09; 'deprecated': 0.09; 'latter': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'stating': 0.09; 'subject:()': 0.09; 'python': 0.11; '[1].': 0.16; 'assumptions': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'sense,': 0.16; 'str,': 0.16; 'urllib': 0.16; 'variable.': 0.16; 'index': 0.16; 'wrote:': 0.18; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; "i've": 0.25; 'source': 0.25; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; '[1]': 0.29; "doesn't": 0.30; 'characters': 0.30; '"",': 0.31; 'didnt': 0.31; 'extract': 0.31; 'omitted': 0.31; 'subject:that': 0.31; 'file': 0.32; 'linux': 0.33; '(most': 0.33; 'could': 0.34; 'problem.': 0.35; 'case,': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'found.': 0.36; 'next': 0.36; 'charset:us-ascii': 0.36; 'area': 0.37; 'to:addr:python-list': 0.38; 'recent': 0.39; 'explain': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'even': 0.60; 'course': 0.61; "you're": 0.61; 'making': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.64; 'mar': 0.68; '2013,': 0.91; 'do:': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dave Angel <davea@davea.name>
Subject Re: Getting a value that follows string.find()
Date Wed, 14 Aug 2013 01:31:58 +0000 (UTC)
References <40816fed-38d4-4baa-92cc-c80cd8febd82@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 174.32.174.30
User-Agent XPN/1.2.6 (Street Spirit ; Linux)
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.557.1376443939.1251.python-list@python.org> (permalink)
Lines 45
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1376443939 news.xs4all.nl 15886 [2001:888:2000:d::a6]:46745
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:52487

Show key headers only | View raw


englishkevin110@gmail.com wrote:

> I know the title doesn't make much sense, but I didnt know how to explain my problem.
>
> Anywho, I've opened a page's source in URLLIB
> starturlsource = starturlopen.read()
> string.find(starturlsource, '<a href="/profile.php?id=')
> And I used string.find to find a specific area in the page's source.
> I want to store what comes after ?id= in a variable.
> Can someone help me with this?

Python 3.3.0 (default, Mar  7 2013, 00:24:38) 
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>> help(string.find)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'find'

There is no find function in the string module [1].  But assuming
starturlsource is a str, you could do:

pattern =  '<a href="/profile.php?id='
index = starturlsource.find( pattern )

index will then be -1 if there's no match, or have a non-negative value
if a match is found.

In the latter case, you can extract the next 17 characters with

newstr = starturlsource[index+len(pattern):index+len(pattern)+17]

You are of course making several assumptions about the web page, which
are perfectly reasonable since it's a page under your control.  Or is
it?


[1]  Assuming Python 3.3 since you omitted stating the version you're
using.  But even in Python 2.7, using the string.find function is
deprecated in favor of the str method.

-- 
DaveA

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


Thread

Getting a value that follows string.find() englishkevin110@gmail.com - 2013-08-13 15:51 -0700
  Re: Getting a value that follows string.find() Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-13 18:58 -0400
    Re: Getting a value that follows string.find() englishkevin110@gmail.com - 2013-08-13 16:03 -0700
      Re: Getting a value that follows string.find() Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-13 19:18 -0400
      Re: Getting a value that follows string.find() Joel Goldstick <joel.goldstick@gmail.com> - 2013-08-13 19:40 -0400
      Re: Getting a value that follows string.find() Steven D'Aprano <steve@pearwood.info> - 2013-08-14 06:29 +0000
  Re: Getting a value that follows string.find() Dave Angel <davea@davea.name> - 2013-08-14 01:31 +0000
  Re: Getting a value that follows string.find() John Gordon <gordon@panix.com> - 2013-08-14 15:58 +0000

csiph-web