Search code examples
pythonweb-scrapingpyquery

Pyquery scrape a class having space in its class name


My problem is that i have to scrape a website which has:

<div class="xyz tab"> blah blah </div>

And another div in the same website having which is as follows:

<div class="xyz">blah blah</div>

to scrape a website without space i can use this

pyquery('.xyz').text()

but for with space what should i do???


Solution

  • If you need to check for both xyz and tab classes, you can use .xyz.tab:

    >>> from pyquery import PyQuery as pq
    >>> data = """
    ... <div>
    ...     <div class="xyz tab">test1</div>
    ...     <div class="xyz">test2</div>
    ... </div>
    ... """
    >>> d = pq(data)
    >>> print d('.xyz.tab')
    <div class="xyz tab">test1</div>