我是lxml和python的新手.我正在尝试解析一个HTML文档.当我使用标准的xml解析器进行解析时,它会正确地写出字符,但我认为它无法解析,因为我无法使用xpath进行搜索.
正在解析的示例文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>title</title>
</head>
<body>
<span id="demo">Garbléd charactérs</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
解析代码:
from lxml import etree
fname = 'output/so-help.html'
# parse
hparser = etree.HTMLParser()
htree = etree.parse(fname, hparser)
# garbled
htree.write('so-dumpu.html', encoding='utf-8')
# targets
demo_name = htree.xpath("//span[@id='demo']")
# garbled
print 'name: "' + demo_name[0].text
Run Code Online (Sandbox Code Playgroud)
终端输出:
name: "Garbléd charactérs
Run Code Online (Sandbox Code Playgroud)
htree.write输出:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>title</title></head><body>
<span id="demo">Garbléd charactérs</span>
</body></html>
Run Code Online (Sandbox Code Playgroud) 当模块未定义为字符串时,我似乎无法进行任何导入.到底是怎么回事?
test.ts
import b = module('Backbone')
Run Code Online (Sandbox Code Playgroud)
不起作用: backbone.d.ts
declare module Backbone {
export class Events {
...
Run Code Online (Sandbox Code Playgroud)
作品: backbone.d.ts
declare module "Backbone" {
export class Events {
...
Run Code Online (Sandbox Code Playgroud)
编辑1:
仅供参考10.1.4
带有StringLiteral的AmbientModuleIdentification声明了一个外部模块.仅在全局模块中允许此类声明.StringLiteral必须指定顶级外部模块名称.不允许使用相对的外部模块名称
我不明白它是如何有用的不作为找到它指定为字符串文本格式在这里和这里.如果您在///<reference...没有字符串文字模块的情况下使用它,但我正在尝试生成依赖于这些库的AMD模块,那么我需要导入才能工作.我是少数,我必须去修改每个.d.ts作为字符串文字版本?
编辑2:
使用字符串文字声明模块要求导入与字符串文字完全匹配.您不能再使用相对或绝对路径来访问此模块/模块定义的位置,即使它与尝试导入它的文件不在同一目录中.这使得a ///<reference和导入是必需的,但产生了一个tsc --module AMD正在寻找的模块(模块的路径由模块字符串文字决定"Backbone").
例如.
+- dep/
|- backbone.d.ts
|- test.ts
Run Code Online (Sandbox Code Playgroud)
backbone.d.ts:
declare module "Backbone" {
export class Events {
Run Code Online (Sandbox Code Playgroud)
作品:test.ts:
///<reference path="../dep/backbone.d.ts" …Run Code Online (Sandbox Code Playgroud) 有人正在使用Backbone.d.ts编写Backbone应用程序.我想讨论两个用例.
对于阵营1中的人员,必须将骨干模块定义为外部模块,以便能够导入模块并将其包含在define()包装器中.
对于阵营2中的人,必须将骨干模块定义为内部模块,以便使用智能感知而不需要使用import语句/ define()包装器.
问题:是否有其他方法来定义模块,以便在两种情况下都可以使用它?
我真的不想创建一个叉子,这样你就可以拥有它
// required for those using import (1)
declare module "Backbone" {
Run Code Online (Sandbox Code Playgroud)
要么
// required for those not using import (2) and backbone already exists in the global scope
declare module Backbone {
Run Code Online (Sandbox Code Playgroud)
并且仍然能够与您的代码/ intellisense相处.
骨干滚动事件不会触发.click事件和jquery附加滚动工作.这也是一个小提琴:http://jsfiddle.net/cX2UV/
JS
var TView = Backbone.View.extend({
events: {
'scroll .content': 'loadMore',
'click .code': 'codeClick'
},
loadMore: function() {
this.$el.append('<p>backbone scrolling</p>');
},
codeClick: function() {
this.$el.append('<p>clicking</p>');
}
});
new TView({ el: '.target' });
$('.content').scroll(function(){ $('.target').append('jq scrolling...'); });
Run Code Online (Sandbox Code Playgroud)
HTML
<div class="target">
<div class="content">
<div>
<span class="text">text</span>
<span class="term">termmm</span>
<span class="codes"><span class="code">12345</span></span>
</div>
<div>
<span class="text">text</span>
<span class="term">termmm</span>
<span class="codes"><span class="code">12345</span></span>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
body {margin:1em;}
.content {background:#EEE; height: 200px; overflow: scroll;}
.code{color:#00c}
Run Code Online (Sandbox Code Playgroud) 我对我的lambda还不太确定但是为什么以下工作没有?4/MVC2
作品:
// SpotlightsController.cs
public class SpotlightFormViewModel
{
// props
public Spotlight Spotlight { get; private set; }
public SelectList Featured { get; private set; }
public IDictionary<string, int> feature = new Dictionary<string, int>(){
{"True", 1},
{"False", 0},
};
// constr
public SpotlightFormViewModel(Spotlight spotlight)
{
Spotlight = spotlight;
Featured = new SelectList(feature.Keys, spotlight.Featured);
}
}
// Edit.aspx
<div class="editor-label">
<label for="Featured">Featured:</label>
</div>
<div class="editor-field">
<%: Html.DropDownList("Featured", Model.Featured)%>
<%: Html.ValidationMessage("Featured") %>
</div>
Run Code Online (Sandbox Code Playgroud)
不起作用:
// Compiler Error Message: CS1501: No overload for …Run Code Online (Sandbox Code Playgroud) 不是循环我的字符串我想使用LINQ.怎么做以下?
// explode our word
List<char> rackBag = new List<char>();
rackBag.AddRange("MYWORD??".ToCharArray());
// How many wildcards?
int wildCardCount = rackBag.Count(x => x.Equals("?"));
Run Code Online (Sandbox Code Playgroud)
wildCardCount应该等于2.
做<和>需要在C#中的正则表达式逃脱?我不确定是因为命名组,因为这个正则表达式备忘单在元字符部分列出了它们.
html = Regex.Replace(html, "(<body.*?>)", replacement);
Run Code Online (Sandbox Code Playgroud)
要么
html = Regex.Replace(html, "(\<body.*?\>)", replacement);
Run Code Online (Sandbox Code Playgroud) 我似乎无法覆盖"no-use-before-define". 我不想在定义之前使用函数时收到警告。我尝试在 VS Code 中禁用并重新启用 ESLint 服务,但没有成功。
.eslintrc
{
"env": {
"browser": true
},
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"],
"extends": [
"airbnb",
"plugin:@typescript-eslint/recommended",
"prettier",
"prettier/@typescript-eslint",
"prettier/react",
"prettier/standard",
"prettier/@typescript-eslint",
"plugin:prettier/recommended"
],
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
},
"rules": {
"no-use-before-define": ["error", { "functions": false, "classes": true }]
}
}
Run Code Online (Sandbox Code Playgroud)
.prettierrc.js
module.exports = {
semi: true,
trailingComma: 'all',
singleQuote: true,
printWidth: 120,
tabWidth: 2,
endOfLine: 'auto',
}
Run Code Online (Sandbox Code Playgroud) 我调用远程数据源并返回json。在我选择一个项目后,select:回调选项只允许我使用项目的标签和值,但我还想使用我的 json 对象的其他属性来自动填充其他字段。
有没有一种方便的方法来做到这一点,我错过了?到目前为止我看到的选项是......
对其中任何一个都不是特别满意。想法?
编辑 我忘了我在使用 $.map
$('#accountName').autocomplete({
source: function (request, response) {
$.getAccountsByNameLike(request.term, function (data) {
response($.map(data, function (item) {
return {
label: item.Name + ' (' + item.Address.City + ', ' + item.Address.StateOrProvince + ')',
value: item.AccountId,
// Added to fix issue
raw: item
}
}));
}, function (error) {
// async kickoff a log to logging server service...
alert("There was a problem while trying to retrieve account names. Please contact support");
});
Run Code Online (Sandbox Code Playgroud) c# ×3
javascript ×3
typescript ×3
jquery ×2
autocomplete ×1
backbone.js ×1
encoding ×1
eslint ×1
jquery-ui ×1
json ×1
linq ×1
lxml ×1
python ×1
regex ×1