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

Posted

技术标签:

【中文标题】比较来自路径和请求的字符串结果【英文标题】:Compare string result from path & requests 【发布时间】:2019-07-05 17:05:39 【问题描述】:

我正在从定义的 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】:

关键字是一个列表。

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

"location" in keyword[0] #gives True

【讨论】:

谢谢伙计!你解决了我的问题,我是 Python 和报废库的新手。

以上是关于比较来自路径和请求的字符串结果的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot常用注解

TypeError [ERR_UNESCAPED_CHARACTERS] 请求路径包含非转义字符

.NET httpClient Post请求,GET请求方法

XACML 政策。应用函数字符串子集给出意外结果

jOOQ:比较 uuid(来自 postgresql)和字符串(类中的用户 id)

带有字段比较结果的django注释查询集