python - 比较路径和请求的字符串结果

标签 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

实际结果:能够检索所有必要的信息,包括其元素和内容

预期结果:向用户打印危险/安全字词并定义变量“Flag”,然后将其存储到数据库中。

最佳答案

关键字是一个列表。

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

"location" in keyword[0] #gives True

关于python - 比较路径和请求的字符串结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54643516/

相关文章:

python - pandas 版本的 SQL CROSS APPLY

Python - 将脚本安装到系统

python - Pandas :聚合后加入分组键

ruby - 为什么 Net:HTTP/HTTPParty 调用中断 Watir-Webdriver 脚本/Net::ReadTimeout 错误

python - 同一个 id 的多个外键。 Django 。设计模式

python - 获取scrapy上不同部分的数据列表

go - 怎么去网上找包裹?

c++ - 如果图形以邻接表的形式给出,如何扫描用户的输入?

android - Android 中的图形二叉树

递归创建树