我正在尝试从任意html页面中提取文本.有些页面(我无法控制)有错误的html或脚本,这使得这很困难.此外,我在一个共享的托管环境,所以我可以安装任何python库,但我不能只在服务器上安装我想要的东西.
pyparsing和html2text.py似乎也不适用于格式错误的html页面.
示例URL是http://apnews.myway.com/article/20091015/D9BB7CGG1.html
我目前的实施大致如下:
# Try using BeautifulSoup 3.0.7a
soup = BeautifulSoup.BeautifulSoup(s)
comments = soup.findAll(text=lambda text:isinstance(text,Comment))
[comment.extract() for comment in comments]
c=soup.findAll('script')
for i in c:
i.extract()
body = bsoup.body(text=True)
text = ''.join(body)
# if BeautifulSoup can't handle it,
# alter html by trying to find 1st instance of "<body" and replace everything prior to that, with "<html><head></head>"
# try beautifulsoup again with new html
Run Code Online (Sandbox Code Playgroud)
如果beautifulsoup仍然不起作用,那么我采用一种启发式方法来查看第一个字符,最后一个字符(看看它们是否看起来像是一个代码行#<;然后取一行代码然后检查是否有令牌是英文单词或数字.如果很少的标记是单词或数字,那么我猜这行是代码.
我可以使用机器学习来检查每一行,但这看起来有点贵,我可能需要训练它(因为我不太了解无监督的学习机器),当然也可以写它.
任何建议,工具和策略都是最受欢迎的.此外,我意识到后一部分相当混乱,因为如果我得到一行确定包含代码,我现在扔掉整行,即使行中有少量实际的英文文本.
我想要求以下内容:
......我想出了这个:
(?=.{6,})(?=(.*\d){2,})(?=(.*\W){2,})
Run Code Online (Sandbox Code Playgroud)
现在,我还想确保没有两个连续字符是相同的.我有点时间让它工作了.这就是我自己得到的东西:
(\S)\1+
Run Code Online (Sandbox Code Playgroud)
...但如果我试图将两者结合在一起,那就失败了.
我在应用程序的限制范围内运行.它的默认要求是1个字符长度,没有正则表达式,也没有非标准字符.
无论如何...
使用这个测试工具,我希望y90e5 $匹配,但y90e5 $ $不匹配.
我错过了什么?
在过去的几天里,我一直在寻找这个问题的解决方案.这是一个简单的烦恼,但我不想知道如何做事......
环境:Ruby,Rails,rhtml
问题:当我在rhtml中迭代一个集合时,我想减少我使用的<%%>和<%=%>标签的数量.以下似乎臃肿:
<% @products.each do |p| %>
<%= @p.name %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
编辑:如何在块内排除<%=%>?
我宁愿这样做:
<% @products.each do |p|
puts @p.name
end %>
Run Code Online (Sandbox Code Playgroud)
某些情况可以允许使用...但是,我知道我可以用jsp做到这一点:
<% for(int i=0; i<10;i++){
System.out.print(i);
} %>
Run Code Online (Sandbox Code Playgroud)
提前感谢您的意见.
我是否必须重写我的代码才能在界面中执行此操作?或者有更简单的方法吗?我正在使用Moq
我有一个XCode项目(A)引用另一个项目(B).默认情况下(据我所知),XCode将隐式构建与目标配置B匹配的依赖项的配置A(例如,"Debug").但是,如果我想要A构建为"Debug"并B构建为"Release",该怎么办?我如何在XCode中指定它?
在Windows XP中,Win32 API使用GDI/GDI +呈现控件.现在我已经7了,所以如果我使用API的功能,渲染会自动由DWM/WDDM处理(所以DirectX)?还是会继续用GDI呈现?
或者同样,用WinAPI编写的旧应用程序是否也会在Windows 7中使用GDI进行渲染?
提前谢谢你的帮助:)
我有一个读取和写入的文件.我需要确保在写入时,没有其他人会尝试写入它.
我对整个函数进行了锁定,允许读取或写入,但仍然会出现错误,例如进程无法访问文件'FILENAME',因为它正由另一个进程使用.
public static TYPE Property{
get{
data mydata;
Object obj = new object();
Monitor.Enter(obj);
// check if data has not changed
// if it has not, just read
using (Stream stream = File.Open(fileLocation, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
//....
}
// else, if data changed, then need to write to file to save the new data
using (Stream stream = File.Open(fileLocation, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) {
BinaryFormatter bf = new BinaryFormatter();
try {
bf.Serialize(stream, (data);
}
//DONE processing
Monitor.Pulse(obj);
Monitor.Exit(obj);
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PowerShell脚本打开SQL数据连接,我的密码包含一个$符号:
$cn = new-object system.data.SqlClient.SqlConnection("Data Source=DBNAME;Initial Catalog=Catagory;User ID=User;Password=pass$word;")
Run Code Online (Sandbox Code Playgroud)
当我尝试打开连接时,它说:
登录失败
我正在尝试从我已经制作成Wordpress主题的插件中添加一些功能,但我没有什么快乐.文档并没有真正帮助我解决问题,所以也许这里的人可以提供帮助.
我在Wordpress中有一个插件,它被激活并正常工作.这个插件的类有一个名为generateHtml的函数,我想从Wordpress主题访问它.但无论我尝试什么,我似乎无法访问我的插件的代码.
可以向我总结一下我需要做些什么才能让主题从插件中访问代码和/或指出我在我的代码中出错了:
插入:
<?php
/** Usual comments here **/
if (!class_exists("ImageRotator")) {
class ImageRotator {
private $uploadPath = '';
private $pluginPath = '';
private $options;
function __construct() {
$this->uploadPath = dirname(__file__).'\\uploads\\';
// add_shortcode('imagerotator', array(&$this, 'generateHtml'));
}
// Various functions for plugin
function generateHtml() {
echo '<p>Hello World</p>';
}
}
}
/**
* Create instance of image rotator
*/
$imageRotator = new ImageRotator();
/**
* Create actions & filters for Wordpress
*/
if (isset($imageRotator)) {
// Actions
add_action('admin_menu', array(&$imageRotator, 'createMenu')); …Run Code Online (Sandbox Code Playgroud) 以下Javascript控制台:
var a = {'foo': []};
var b = {};
for (var key in a) {
b[key] = a[key];
}
a['foo'].push(1);
console.log(b);
Run Code Online (Sandbox Code Playgroud)
产量:
Object foo=[1]
我想为a中的每个键的每个数组的b中的值创建一个副本.有没有更简单的方法?