Change language

Python | Parse a site using regular expressions and urllib

| | |

Please note that parsing websites means they are pulling all source code and that we want to search on a given url, it will give you output as the bulk of HTML content that you cannot understand ... Let’s take a look at a demo with an explanation so you can understand more about parsing.

Code # 1: libraries are required

# import libraries

import urllib.request

import urllib.parse

import re

Code # 2:

url = https://python.engineering/

values ​​ = { ’s’ : ’ python programming’ ,

’submit’ : ’search’ }

We have defined the URL and some related values ​​that we want to find. Remember that we are defining values ​​as a dictionary, and in this key-value pair, we are defining programming in Python to search at a specific URL.

Code # 3:

data = urllib.parse.urlencode (values) 

data = data.encode ( ’utf-8’

req = urllib.request.Request (url, data) 

resp = urllib.request .urlopen (req) 

 

respData = resp.read () 

On the first line, we encode the values ​​that we defined earlier, then (line 2) we encode the same data that the machine understands. 
In the third line of code, we ask for the values ​​at a specific URL urlopen () and then use the urlopen () module to open the HTML web document. 
In the last line, read () will help you read the document line by line and assign it to the named variable respData .

Code # 4:

paragraphs = re.findall (r ’"p" (. *?) "/ p"’ , str (respData))

 

for eachP in paragraphs:

print (eachP)

We use a regular expression to retrieve the relevant data. The second argument must be a type string, and if we want to print the data, we use a simple print function.

Here are some examples:

Example # 1:

Output:

Example # 2:

import urllib.request

import urllib.parse

import re

 

url = https://python.engineering/

values ​​ = { ’s’ : ’ python programming ’ ,

’submit’ : ’search’ }

 

data = urllib.parse.urlencode (values)

data = data.encode ( ’utf-8’ )

req = urllib.request.Request (url, data)

resp = urllib.request.urlopen (req)

respData = resp.read ()

 

par agraphs = re.findall (r ’"p" (. *?) "/ p" ’ , str (respData) )

 

for eachP in paragraphs:

  print (eachP)

import urllib.request

import urllib.parse

import re

  

url = https://python.engineering/

values ​​ = { ’s ’ : ’ pandas’ ,

’submit’ : ’ search’ }

 

data = urllib.parse.urlencode (values)

data =  data.encode ( ’utf-8’ )

req = urllib.request.Request (url, data)

resp = urllib.request.urlopen (req)

respData = resp.read ()

 

paragraphs = re.findall (r ’"p" (. *?) "/ p"’ , str (respData))

  

for eachP in paragraphs:

  print (eachP)

Output:

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News


Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method