简体   繁体   English

Python 3-逐行阅读html并找到正确的单词

[英]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. 例如,我使用此代码来获取第55至70行的源代码,然后使用if语句在本节中找到特定的单词。

Get lines from 55 to 70: 获取55至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)] . 然后,您发现在line[index:index + len(something)]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM