Сравнить строку результата из пути и запросов - PullRequest
0 голосов
/ 12 февраля 2019

Я извлекаю HTML-код из определенного URL-адреса, в основном фокусируясь на теге, чтобы извлечь его результаты.Затем сравните, если в сценарии существует строка «example», если да, напечатайте что-нибудь и установите флаг = 1.

Я не могу сравнить результаты, извлеченные из HTML.fromstring

Ableудалить содержимое 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

Фактический результат: возможность получить всю необходимую информацию, включая ее элемент и содержимое

Ожидаемый результат: напечатать пользователю слово ОПАСНОСТЬ / БЕЗОПАСНОСТЬ и определить переменную «Флаг», которая затем будет сохранена в базе данных.

1 Ответ

0 голосов
/ 12 февраля 2019

ключевое слово - это список.

Вам нужно проиндексировать список, чтобы получить строку, после которой вы сможете искать конкретную строку

"location" in keyword[0] #gives True
...