python - 具有索引输出的 Find_between 函数?
问题描述
我想使用该find_between
函数从特定的 Web 服务器检索可索引的值。
我正在使用该requests
模块从第 18 行看到的特定网站收集一些源代码:
response = requests.get("https://www.shodan.io/search?query=Server%3A+SQ-WEBCAM")
我想调用该find_between
函数以使用指定的参数检索所有值(页面上的所有项目,每个项目由“n”的递增值表示)find_between
:
x = find_between(response.content,'/></a><a href="/host/','">---')
有谁知道如何解决这个问题?
import sys
import requests
from time import sleep
# Find between page tags on page.
def find_between( s, tag1, tag2 ):
try:
start = s.index( tag1 ) + len( tag1 )
end = s.index( tag2, start )
return s[start:end]
except ValueError:
return ""
def main():
# Default value for 'n' index value (item on page) is 0
n = 0
# Enter the command 'go' to start
cmd = raw_input("Enter Command: ")
if cmd == "go":
print "go!"
# Go to this page for page item gathering.
response = requests.get("https://www.shodan.io/search?query=Server%3A+SQ-WEBCAM")
# Initial source output...
print response.content
# Find between value of 'x' sources between two tags
x = find_between(response.content,'/></a><a href="/host/','">---')
while(True):
# Wait one second before continuing...
sleep(1)
n = n + 1
# Display find_between data in 'x'
print "\nindex: %s\n\n%s\n" % (n, x)
# Enter 'exit' to exit script
if cmd == "exit":
sys.exit()
# Recursive function call
while(True):
main()
解决方案
您的代码中的一些事情似乎需要解决:
- 的值
x
设置在循环之外(之前)while
,因此循环会增加索引n
,但会一遍又一遍地打印相同的文本,因为x
永远不会改变。 find_between()
仅返回一个匹配项,并且您想要所有匹配项。- 你的
while
循环永远不会结束。
建议:
- 将调用
find_between()
放在while
循环内。 - 每次连续调用
find_between()
时,只传递上一个匹配项之后的文本部分。 - 找不到匹配项时退出
while
循环。find_between()
像这样的东西:
text_to_search = response.content
while(True):
# Find between value of 'x' sources between two tags
x = find_between(text_to_search, '/></a><a href="/host/', '">---')
if not x:
break
# Wait one second before continuing...
sleep(1)
# Increment 'n' for index value of item on page
n = n + 1
# Display find_between data in 'x'
print "\nindex: %s\n\n%s\n" % (n, x)
# Remove text already searched
found_text_pos = text_to_search.index(x) + len(x)
text_to_search = text_to_search[found_text_pos:]
推荐阅读
- node.js - Azure 应用服务 - 带有 CORS 的 RESTful API
- python - sys 模块内置在每个 python 解释器中是什么意思?
- angular - 使用 go 提供 angular dist 文件夹
- android - Firebase 数据库在值更改时重新打开活动
- javascript - 获取所选文本开头和结尾的字符偏移量
- laravel - laravel 事件应该广播不起作用
- jquery - jQuery:将值设置为选择输入字段
- amazon-web-services - 推送通知的 Amazon SNS android 设备令牌限制
- node.js - `sass-loader` 需要 `node-sass` >=4。请安装兼容版本
- git - 如何在 git repo 中查找现有的旧文件