navid
navid

Reputation: 1

Python 3 - Reading html line by line and finding the right word

import urllib.request
url = "site.com"
request = urllib.request.Request(url)
my = urllib.request.urlopen(request)
print (my.read().decode('utf-8'))

I used this code, for example, to get the source code for lines 55 to 70 and then find a specific word in this section using if statements.

Upvotes: 0

Views: 119

Answers (2)

Alyssa Haroldsen
Alyssa Haroldsen

Reputation: 3731

finding = '<sometag>'
text = my.read().decode('utf-8').splitlines()[54:70] # Include Line 55
pos = text.find(finding)
if pos != -1:
    # Do what you need to

Upvotes: 0

cdonts
cdonts

Reputation: 9599

Get lines from 55 to 70:

lines = my.read().decode('utf-8').split("\n")[55:70]

Find something:

for line in lines:
    index = line.find(something)
    if index > -1:
        # ...

Then what you've find is in line[index:index + len(something)].

Upvotes: 2

Related Questions