比较来自路径和请求的字符串结果

时间:2019-02-12 05:42:07

标签: python web-scraping tree lxml lxml.html

我正在从定义的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”,然后将其存储到数据库中。

1 个答案:

答案 0 :(得分:0)

关键字是一个列表。

您需要索引列表以获取字符串,然后才能搜索特定的字符串

"location" in keyword[0] #gives True