我试图从下面提到的网址中提取一个link使用xpath
string url = "http://www.album-cover-art.org/search.php?q=Ruin+-+Live+Album+Version+Lamb+of+God"
Run Code Online (Sandbox Code Playgroud)
我的代码:
HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc = web.Load(url); //Exception generated here Line 23
if (htmlDoc.DocumentNode != null)
{
HtmlNode linkNode = htmlDoc.DocumentNode.SelectSingleNode(".//*[@id='related_search_row']/img/@src");
if (linkNode != null)
Console.WriteLine(linkNode.InnerText);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码编译得很好,但是当我尝试运行它时会产生异常
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
Run Code Online (Sandbox Code Playgroud)
完成堆栈跟踪
System.NullReferenceException: Object reference not set to an instance of an object.
at HtmlAgilityPack.HtmlDocument.ReadDocumentEncoding(HtmlNode node) in C:\Source\htmlagilitypack\Trunk\HtmlAgilityPack\HtmlDocument.cs:line 1916
at HtmlAgilityPack.HtmlDocument.PushNodeEnd(Int32 index, Boolean close) in …Run Code Online (Sandbox Code Playgroud) 假设我在不同场景中有2个班级.
场景1
class MyClass():
temp = 5
Run Code Online (Sandbox Code Playgroud)
情景2
class MyClass():
temp = 5
def myfunc(self):
print self.temp
Run Code Online (Sandbox Code Playgroud)
现在什么时候将变量temp视为类变量和实例变量.我很困惑,因为在这两种情况下我都可以temp使用两者来访问变量的值.
Object.Temp (表现为实例变量)
ClassName.Temp (表现为类变量)
我相信之前已经提出了类似的问题,但如果有人能够在我的问题的背景下解释这个问题,这将是一个很大的帮助.
我正在试验chrome.extensionAPI.
的manifest.json
{
"name": "First",
"version": "1.0",
"manifest_version": 2,
"description": "First extension",
"background": {
"scripts": ["test.js"]
},
"page_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [ {
"js": [ "jquery.min.js", "display.js" ],
"matches": [ "http://*/*", "https://*/*" ]
} ],
"permissions" : [
"tabs",
"http://*/*", "https://*/"
]
}
Run Code Online (Sandbox Code Playgroud)
display.js
alert("inside display.js");
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
alert("inside msg");
var time = request.sel_text;
alert(time);
});
Run Code Online (Sandbox Code Playgroud)
test.js
function check(tab_id, data, tab){
if(tab.url.indexOf("google") > -1){
chrome.pageAction.show(tab_id);
chrome.tabs.executeScript(null, {"file" : "display.js"}) ;
}
}; …Run Code Online (Sandbox Code Playgroud) 我正在尝试从下面给出的字符串中提取一些信息
>>> st = '''
... <!-- info mp3 here -->
... 192 kbps<br />2:41<br />3.71 mb </div>
... <!-- info mp3 here -->
... 3.49 mb </div>
... <!-- info mp3 here -->
... 128 kbps<br />3:31<br />3.3 mb </div>
... '''
>>>
Run Code Online (Sandbox Code Playgroud)
现在当我使用下面的正则表达式我的输出是
>>> p = re.findall(r'<!-- info mp3 here -->\s+(.*?)<br />(.*?)<br />(.*?)\s+</div>',st)
>>> p
[('192 kbps', '2:41', '3.71 mb'), ('128 kbps', '3:31', '3.3 mb')]
Run Code Online (Sandbox Code Playgroud)
但我要求的输出是
[('192 kbps', '2:41', '3.71 mb'),(None,None,'3.49mb'), ('128 kbps', '3:31', '3.3 mb')]
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是如何更改我的上述内容regex …
我有一个list和list of list这样的
>>> list2 = [["1","2","3","4"],["5","6","7","8"],["9","10","11","12"]]
>>> list1 = ["a","b","c"]
Run Code Online (Sandbox Code Playgroud)
我压缩了上面的两个列表,以便我可以通过索引匹配它们的值索引.
>>> mylist = zip(list1,list2)
>>> mylist
[('a', ['1', '2', '3', '4']), ('b', ['5', '6', '7', '8']), ('c', ['9', '10', '11', '12'])]
Run Code Online (Sandbox Code Playgroud)
现在我尝试打印上面的输出mylist使用
>>> for item in mylist:
... print item[0]
... print "---".join(item[1])
...
Run Code Online (Sandbox Code Playgroud)
它产生了这个输出,这是我的desired output.
a
1---2---3---4
b
5---6---7---8
c
9---10---11---12
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是有更多的cleaner and better方法来实现我想要的输出或这是best(short and more readable)可能的方式.
我正在尝试python的difflib模块,我遇到了SequenceMatcher.所以,我尝试了以下示例,但无法理解发生了什么.
>>> SequenceMatcher(None,"abc","a").ratio()
0.5
>>> SequenceMatcher(None,"aabc","a").ratio()
0.4
>>> SequenceMatcher(None,"aabc","aa").ratio()
0.6666666666666666
Run Code Online (Sandbox Code Playgroud)
现在,根据比例:
将序列相似性的度量返回为[0,1]范围内的浮点数.
T两个序列中元素的总数 在哪里M,匹配的数量是多少2.0*M / T.
所以,对于我的情况:
T=4和M=1因此比2*1/4 = 0.5T=5和M=2因此比2*2/5 = 0.8T=6和M=1因此比2*1/6.0 = 0.33根据我的理解T = len(aabc) + len(a),M=2因为a两次进来aabc.
那么,我错在哪里,我错过了什么.
这是源代码SequenceMatcher.ratio()
If下面给出的逻辑在C语言中运行良好,但它在java中不起作用....为什么......?
它在编译时在java中出错.
class test
{
public static void main(String[] args)
{
int i;
if(i=4)
System.out.println("hello");
}
}
Run Code Online (Sandbox Code Playgroud) 我在想,有没有什么方法可以通过我的登录我的stackoverflow.com帐户python code.
有什么stackoverflow-pythonAPI我可以通过它完成上述任务.
我在问这里之前用Google搜索,但是找不到任何API,也没有任何Hello World使用python的例子.
我正在尝试whitespaces在将其与正则表达式匹配后替换为字符串.
my $string = "watch download Buffy the Vampire Slayer Season 1 Episode 1 Gorillavid";
if ($string =~ m!(Season\s\d+\sEpisode\s\d+)!){
$1 =~ s!\s+!!g;
say $1;
}
Run Code Online (Sandbox Code Playgroud)
现在,当我运行上面的代码时,我得到了Modification of a read-only value attempted.现在,如果我将值存储$1在变量中,而不是尝试对该变量执行替换,则可以正常工作.
那么,有没有什么方法可以在不创建新临时变量的情况下执行替换.
PS:有人可以告诉我如何将上面的代码写成一行,因为我无法:)
我删除了一个文件git rm test,目前这是输出git status .
Noob@Noob:/media/data/bitbucket/pythonscripts$ git status .
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# deleted: test
#
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试使用git push它来推动我的回购中的更改时说Everything up-to-date.我理解通常需要先按推送而add不是commit推送,但在我的情况下,我添加了什么,因为我已经删除了文件.
此外,如果我这样做git commit -m "commit message"将覆盖我的所有提交,因为目前我没有文件附加此提交.
python ×5
regex ×2
bitbucket ×1
c# ×1
git ×1
github ×1
if-statement ×1
java ×1
javascript ×1
list ×1
perl ×1
similarity ×1
string ×1