loading...
Cover image for Web scraping with PyQuery

Web scraping with PyQuery

petercour profile image petercour ・1 min read

How do you do webscraping these days? You may be working with beautifulsoup or automate the web browser with selenium.

If you have very basic scraping needs, you could consider pyquery.

pyquery allows you to make jquery queries on xml documents. That's great, because you can use it on HTML.

First intsall PyQuery with pip. Then you can use it like this:

#!/usr/bin/python3
from pyquery import PyQuery as pq

doc =pq(url = "https://pythonbasics.org")
print( doc('title').text() )

That will grab the title from the web page.
Want to get all links from a web page?

#!/usr/bin/python3
from pyquery import PyQuery as pq

doc =pq(url = "https://dev.to")

for link in doc('a'):
    print(link.attrib['href'])

Easy right?

Do you prefer getting images?

#!/usr/bin/python3
from pyquery import PyQuery as pq

doc =pq(url = "https://dev.to")

for link in doc('img'):
    print(link.attrib['src'])

Related links:

Posted on by:

Discussion

markdown guide
 

How is it different from BeautifulSoup?

 

As far as what it does? Doesn't seem to be different at all. It just looks like the HTML parsing and web element selector syntax would be more comfortable for developers coming from the front end than from the back. IME, web scrapers are either front end devs that want to use web scraping for automated front end QA, or are back end devs that use it to collect data for a data set or API they want to build. BeautifulSoup is very pythonic in it's use and, if you're new to Python from the front end, using it might be a bit of a tough gear change. This library simply looks like a bridge to allow JS/JQuery folks to more easily break into Python web scraping comfortably. Sure there are ways to perform web scraping tasks in JS, but this fills a nice little niche for "new transfers" 😁😁

 

PyQuery is as much as possible the similar to jquery. Functionally you can do the same thing, but a different syntax.

 
Sloan, the sloth mascot Comment marked as low quality/non-constructive by the community View code of conduct

Check out I'm using Scrapy tool in python!!!
fiverr.com/m_waqarsikandar