小编Lea*_*AWK的帖子

如果没有用户输入发送到数据库,是否存在任何注入风险?

我有一个小的MySQL数据库,有几百行(全部是文本,没有图像).我使用iQuery请求所有行,并在客户端进行所有过滤.iQuery代码如下:

$(document).ready( function () {
     $.get("alldata.php", function(data){
         $('#result').text(data);
     });  
});
Run Code Online (Sandbox Code Playgroud)

在服务器端,"alldata.php"具有以下代码并将JSON中的数据传递回iQuery:

$sql = "SELECT title FROM mydatabase";
$result =  mysqli_query($conn, $sql);
$arr = array(); 

while($row = mysqli_fetch_assoc($result)){
    $row_array['Title'] =$row['title'];
    array_push($arr,$row_array);
}
mysqli_close($conn);

echo json_encode($arr);
Run Code Online (Sandbox Code Playgroud)

在我看来,由于没有提交给数据库的用户输入,因此不存在任何注入风险.我是对还是错?非常感谢您的投入!

javascript php mysql sql-injection

5
推荐指数
1
解决办法
90
查看次数

Xpath:为什么normalize-space无法删除空白和\ n?

对于以下代码:

<a class="title" href="the link">
Low price
<strong>computer</strong>
you should not miss
</a>
Run Code Online (Sandbox Code Playgroud)

我使用此xpath代码进行抓取:

response.xpath('.//a[@class="title"]//text()[normalize-space()]').extract()
Run Code Online (Sandbox Code Playgroud)

我得到以下结果:

u'\n                  \n                  Low price ', u'computer', u' you should not miss'
Run Code Online (Sandbox Code Playgroud)

为什么\n在此示例中low price没有删除之前的两个空白空间normalize-space()

另一个问题:如何将这三个零件合并为一个刮板u'Low price computer you should not miss'

xpath scrapy-spider

5
推荐指数
1
解决办法
1454
查看次数

xpath:如何提取 &lt;strong&gt; 元素之前、之内和之后的文本

我正在研究一个 Scrapy 蜘蛛,其中 xpath 用于提取所需的信息。源页面首先是通过使用网站的搜索功能生成的。例如,我的兴趣是获取标题中带有“计算机”的项目。在源页面上,由于搜索过程,所有“计算机”都以粗体显示。并且“计算机”可以在标题的开头、中间或结尾。有些项目的标题中没有“计算机”。请参阅以下示例:

Example 1: ("computer" at the beginning)
<a class="title" href="whatever1">
<strong> Computer </strong>
, used
</a>  

Example 2: ("computer" in the middle)
<a class="title" href="whatever2">
Low price
<strong> computer </strong>
, great deal
</a> 

Example 3: ("computer" at the end)
<a class="title" href="whatever3">
Don't miss this
<strong> Computer </strong>
</a>

Example 4: (no keyword of "computer")
<a class="title" href="whatever4">
Best laptop deal ever!      
</a>
Run Code Online (Sandbox Code Playgroud)

我试过的 xpath 代码.//a[@class="title"]/text()只会生成元素之后strong部分。对于上面的4个例子,我会得到以下结果:

Example …
Run Code Online (Sandbox Code Playgroud)

python xpath scrapy web-scraping

4
推荐指数
1
解决办法
1600
查看次数

python:如何将“x天前”或“x个月前”转换为日期?

如何在Python中将以下语句转换为日期(不需要时间)?

10 days ago
about 2 months ago
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止所做的代码:

import re
from datetime import datetime, timedelta

def string_to_delta(s):
    value = int(re.search(r'\d+', s).group())
    if "day" in s:
        value = value*1
    elif "month" in s:
        value = value *30   
    date_ago = (datetime.now() - timedelta(days=value)).date() 
    str_date= date_ago.strftime("%d %B %Y")
    return str_date
Run Code Online (Sandbox Code Playgroud)

它有效,但我感觉很尴尬。

python date

0
推荐指数
1
解决办法
1770
查看次数