• [^] # Re: Avec w3m et grep

    Posté par (site web personnel) . En réponse au message récupérer une info variable sur un site avec CURL. Évalué à 2. Dernière modification le 12 septembre 2016 à 20:04.

    Ah je me doutais que j'aurais mieux fait de commenter mon script de 2 lignes (les 2 premières lignes servent à dire que c'est une script Python, on peut mettre tous les import sur une seule ligne, et on peut grouper les 3 dernières lignes en une seule, mais c'est peu lisible).

    Donc je vais sur le site leboncoin, je regarde le code source de la page (CTRL U avec Firefox par exemple), je cherche

    trouver la bonne affaire parmi

    il n'y en a qu'un, et donc je vois

    <section id="home" class="grid-3-1">
     <section class="mapFrance">
     <header class="mapFrance_header">
     <h1 class="mapFrance_title no-border">Trouvez la bonne affaire parmi <span class="orange">25 402 168 petites annonces</span> sur leboncoin.</h1>
     <section class="homeColLeft">
    

    BeautifulSoup a créé un arbre facile à utiliser, le plus simple, tu lances un prompt Python (python, bpython, Ipython...), tu tapes une à une les commandes, et cela donne

    >>> soup = BeautifulSoup.BeautifulSoup(a.text)
    >>> soup.h1
    <h1 class="mapFrance_title no-border">Trouvez la bonne affaire parmi <span class="orange">25 395 326 petites annonces</span> sur leboncoin.</h1>
    >>> soup.h1.span
    <span class="orange">25 395 326 petites annonces</span>
    >>> dir(soup.h1.span)
    ['HTML_FORMATTERS', 'XML_FORMATTERS', '__call__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattr__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__module__', '__ne__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_all_strings', '_attr_value_as_string', '_attribute_checker', '_find_all', '_find_one', '_formatter_for_name', '_is_xml', '_lastRecursiveChild', '_last_descendant', '_select_debug', '_selector_combinators', '_should_pretty_print', '_tag_name_matches_and', 'append', 'attribselect_re', 'attrs', 'can_be_empty_element', 'childGenerator', 'children', 'clear', 'contents', 'decode', 'decode_contents', 'decompose', 'descendants', 'encode', 'encode_contents', 'extract', 'fetchNextSiblings', 'fetchParents', 'fetchPrevious', 'fetchPreviousSiblings', 'find', 'findAll', 'findAllNext', 'findAllPrevious', 'findChild', 'findChildren', 'findNext', 'findNextSibling', 'findNextSiblings', 'findParent', 'findParents', 'findPrevious', 'findPreviousSibling', 'findPreviousSiblings', 'find_all', 'find_all_next', 'find_all_previous', 'find_next', 'find_next_sibling', 'find_next_siblings', 'find_parent', 'find_parents', 'find_previous', 'find_previous_sibling', 'find_previous_siblings', 'format_string', 'get', 'getText', 'get_text', 'has_attr', 'has_key', 'hidden', 'index', 'insert', 'insert_after', 'insert_before', 'isSelfClosing', 'is_empty_element', 'name', 'namespace', 'next', 'nextGenerator', 'nextSibling', 'nextSiblingGenerator', 'next_element', 'next_elements', 'next_sibling', 'next_siblings', 'parent', 'parentGenerator', 'parents', 'parserClass', 'parser_class', 'prefix', 'prettify', 'previous', 'previousGenerator', 'previousSibling', 'previousSiblingGenerator', 'previous_element', 'previous_elements', 'previous_sibling', 'previous_siblings', 'recursiveChildGenerator', 'renderContents', 'replaceWith', 'replaceWithChildren', 'replace_with', 'replace_with_children', 'select', 'setup', 'string', 'strings', 'stripped_strings', 'tag_name_re', 'text', 'unwrap', 'wrap']
    >>> soup.h1.span.text
    u'25 395 326 petites annonces'
    >>> soup.h1.span.text[:-17]
    u'25 395 326'
    >>> 
    

    la commande dir(xxx) montre les méthodes disponibles, et donc j'ai
    text
    qui me renvoie juste le texte

    ウィズコロナ