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

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically