我有一个小的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)
在我看来,由于没有提交给数据库的用户输入,因此不存在任何注入风险.我是对还是错?非常感谢您的投入!
对于以下代码:
<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'?
我正在研究一个 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中将以下语句转换为日期(不需要时间)?
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)
它有效,但我感觉很尴尬。