比较来自路径和请求的字符串结果
Compare string result from path & requests
我正在从 URL 定义的 HTML 代码中抓取代码,主要集中在标签上,以提取它的结果。然后,比较脚本中是否存在字符串“example”,如果存在,则打印一些内容并标记=1.
我无法比较从 HTML.fromstring
中提取的结果
能够抓取 HTML 内容并成功查看完整内容,想继续但无法(比较字符串)
import requests
from lxml import html
page = requests.get("http://econpy.pythonanywhere.com/ex/001.html")
tree = html.fromstring(page.text) #was page.content
# To get all the content in <script> of the webpage
scripts = tree.xpath('//script/text()')
# To get line of script that contains the string "location" (text)
keyword = tree.xpath('//script/text()[contains(., "location")]')
# To get the element ID of the script that contains the string "location"
keywordElement = tree.xpath('//script[contains(., "location")]')
print('\n<SCRIPT> is :\n', scripts)
# To print the Element ID
print('\n\KEYWORD script is discovered @ ',keywordElement)
# To print the line of script that contain "location" in text form
print('Supporting lines... \n\n',keyword)
# ******************************************************
# code below is where the string comparison comes in
# to compare the "keyword" and display output to user
# ******************************************************
string = "location"
if string in keyword:
print('\nDANGER: Keyword detected in URL entered')
Flag = "Detected" # For DB usage
else:
print('\nSAFE: Keyword does not exist in URL entered')
Flag = "Safe" # For DB usage
# END OF PROGRAM
实际结果:能够检索到所有必要的信息,包括其元素和内容
预期结果:向用户打印 DANGER / SAFE 字样并定义变量“Flag”,然后将其存储到数据库中。
关键字是一个列表。
您需要对列表进行索引以获取字符串,之后您将能够搜索特定的字符串
"location" in keyword[0] #gives True
我正在从 URL 定义的 HTML 代码中抓取代码,主要集中在标签上,以提取它的结果。然后,比较脚本中是否存在字符串“example”,如果存在,则打印一些内容并标记=1.
我无法比较从 HTML.fromstring
中提取的结果能够抓取 HTML 内容并成功查看完整内容,想继续但无法(比较字符串)
import requests
from lxml import html
page = requests.get("http://econpy.pythonanywhere.com/ex/001.html")
tree = html.fromstring(page.text) #was page.content
# To get all the content in <script> of the webpage
scripts = tree.xpath('//script/text()')
# To get line of script that contains the string "location" (text)
keyword = tree.xpath('//script/text()[contains(., "location")]')
# To get the element ID of the script that contains the string "location"
keywordElement = tree.xpath('//script[contains(., "location")]')
print('\n<SCRIPT> is :\n', scripts)
# To print the Element ID
print('\n\KEYWORD script is discovered @ ',keywordElement)
# To print the line of script that contain "location" in text form
print('Supporting lines... \n\n',keyword)
# ******************************************************
# code below is where the string comparison comes in
# to compare the "keyword" and display output to user
# ******************************************************
string = "location"
if string in keyword:
print('\nDANGER: Keyword detected in URL entered')
Flag = "Detected" # For DB usage
else:
print('\nSAFE: Keyword does not exist in URL entered')
Flag = "Safe" # For DB usage
# END OF PROGRAM
实际结果:能够检索到所有必要的信息,包括其元素和内容
预期结果:向用户打印 DANGER / SAFE 字样并定义变量“Flag”,然后将其存储到数据库中。
关键字是一个列表。
您需要对列表进行索引以获取字符串,之后您将能够搜索特定的字符串
"location" in keyword[0] #gives True