我正在使用一个简单的HTMLParser来解析一个网页,其代码总是格式正确(它是自动生成的).它运行良好,直到它碰到一个带有'&'符号的数据 - 它似乎认为这使它成为两个独立的数据并分别处理它们.(也就是说,它会两次调用"handle_data".)我起初认为,取消'&'会解决问题,但我认为不会.有没有人对如何让我的解析器进行处理有任何建议,例如"Paradise Bakery and Cafe"(即"Paradise Bakery&Café")作为单个数据项而不是两个?
非常感谢,bsg
PS请不要告诉我,我真的应该使用BeautifulSoup.我知道.但在这种情况下,我知道标记每次都保证格式良好,我发现HTMLParser比BeautifulSoup更容易使用.谢谢.
我正在添加我的代码 - 谢谢!
#this class, extending HTMLParser, is written to process HTML within a <ul>.
#There are 6 <a> elements nested within each <li>, and I need the data from the second
#one. Whenever it encounters an <li> tag, it sets the 'is_li' flag to true and resets
#the count of a's seen to 0; whenever it encounters an <a> tag, it increments the count
#by 1. When handle_data is called, …Run Code Online (Sandbox Code Playgroud) 我试图在c ++中为我创建的结构类型实现最小堆.我创建了一个类型的向量,但是当我在其上使用make_heap时崩溃了,这是可以理解的,因为它不知道如何比较堆中的项目.如何为结构类型创建最小堆(即顶部元素始终是堆中的最小元素)?
结构如下:
struct DOC{
int docid;
double rank;
};
Run Code Online (Sandbox Code Playgroud)
我想使用rank成员比较DOC结构.我该怎么做?
我尝试使用带有比较器类的优先级队列,但也崩溃了,使用数据结构似乎很愚蠢,当我真正需要的是堆时,使用堆作为其底层基础.
非常感谢,bsg
我正在使用Selenium WebDriver来获取网站的内容.(注意:该站点没有API.希望它能.)该站点使用AJAX在用户滚动时动态加载内容.为了获得该内容,我一直在使用Javascript向下滚动,然后尝试使用findElements()访问内容.
为了清楚设置,页面包含几个嵌套元素,其中一个是带有"GridItems"类的div(没有名称或id).这个div包含许多具有"Item"类的子元素(同样,没有名称或id,只有类).我想在div中使用类"Item"获取每个元素.页面首次加载时可以访问大约25个项目(在当前窗口中不一定可见,但在DOM中可用),向下滚动可以加载更多项目.
我的主要问题如下:首先,当我到达底部时,我想停止滚动.但是,我无法弄清楚使用什么停止条件.如何确定何时到达页面底部?Window.scrollheight将不起作用,因为它将给出现有窗口的高度,而不是它添加更多内容后的高度.我曾考虑测试页面底部的元素是否可见/可点击,但如果不是,则可能仅仅因为它尚未加载,而不是因为尚未到达.即使使用Wait也可能不起作用,因为如果它超时,我不知道是不是因为它没有到达底部,或者只是因为它需要很长时间才能加载.
第二个问题是,当我向下滚动时,它会加载更多元素,但最终,向下滚动会从底部加载更多元素并删除顶部的DOM.这意味着我不能只是向下滚动到底部,然后使用findElements()来获取所有项目,因为许多第一个项目将会消失.我知道会有多少项目,所以目前我正在做以下事情:
int numitems = 135;
List<WebElement> newitems;
List<WebElement> allitems = new ArrayList<WebElement>(50);
do {
//scroll down the full length of the visible window three times
for(int i=0; i < 3; i++)
{
//scroll down
js.executeScript("window.scrollTo(0, document.body.offsetHeight)");
}
//check how many items are now available
//if it runs too fast, it may get to the next line before it finishes scrolling;
//make it wait until the desired div is visible
WebElement cont = (new WebDriverWait(driver, 100))
.until(ExpectedConditions.presenceOfElementLocated(By.className("GridItems"))); …Run Code Online (Sandbox Code Playgroud) 我有一个使用Selenium Webdriver的应用程序从站点获取一些信息.它适用于FirefoxDriver和ChromeDriver,但当我尝试切换到PhantomJSDriver时,遇到了一些困难.
2014年1月5日下午7:28:43 org.apache.http.impl.client.DefaultRequestDirector tryEx ecute INFO:处理请求时捕获的I/O异常(org.apache.http.NoHttpResponseException):目标服务器无法响应
这可能会重复几百次,持续大约10分钟,直到它最终加载页面为止; 有时甚至根本无法加载它.
线程"thread1"中的异常org.openqa.selenium.remote.UnreachableBrowserException:无法启动新会话.可能的原因是远程服务器的无效地址或浏览器启动失败.构建信息:版本:'未知',修订版:'未知',时间:'未知'系统信息:主机:'pangolin',ip:'128.238.32.20',os.name:'Linux',os.arch:' amd64',os.version:'2.6.32-39-generic',java.version:'1.7.0'驱动程序信息:driver.version:PhantomJSDriver at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java :548)org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:216)org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:111)atg.openqa.selenium.remote.RemoteWebDriver .(RemoteWebDriver.java:115)org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:107)org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:96)引起:org.openqa .selenium.WebDriverException:等待驱动程序服务器启动超时.构建信息:版本:'未知',修订版:'未知',时间:'未知'系统信息:主机:'pangolin',ip:'128.238.32.20',os.name:'Linux',os.arch:' amd64',os.version:'2.6.32-39-generic',java.version:'1.7.0'驱动程序信息:driver.version:PhantomJSDriver at org.openqa.selenium.remote.service.DriverService.start(DriverService) .java:165)org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:62)at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:527)... 7更多引起:org.openqa.selenium.net.UrlChecker $ TimeoutException:等待[http:// localhost:16050/status]在org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker)20002 ms后可用.java:104)at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:163)... 9更多引起:com.google.common.util.concurrent.UncheckedTimeoutException:java.util. com.google.common.util.concurrent.Simp上的concurrent.TimeoutException leTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:79)... 10更多引起:java.util.concurrent的java.util.concurrent.TimeoutException .futureTask $ Sync.innerGet(FutureTask.java:258)at java.util.concurrent.FutureTask.get(FutureTask.java:119)at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130 )......还有11个
我究竟做错了什么?我已经阅读了很多关于Phantomjs如何比其他驱动程序快得多的内容,并且非常想使用它,但是如果加载每个页面需要10分钟,那显然是不可行的.
我正在运行Selenium WebDriver版本2.38.0和Phantomjs版本1.9.2.
非常感谢你提前,bsg
编辑 只是为了澄清,我不认为这与我的代码有任何关系; Linux上的错误正在我试图启动PhantomJS驱动程序的行中抛出,如下所示.
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
phantombinary//"/home/p/phantomjs-1.9.2-linux-x86_64/bin/phantomjs"
);
// Launch driver (will take care and ownership of the phantomjs process)
WebDriver driver = new PhantomJSDriver(caps);
System.out.println("starting driver");
Run Code Online (Sandbox Code Playgroud) 我正在使用C++对几个大文件进行排序.我有一个文本文件,其中包含所有输入文件的名称,每行一个.我想一次读取一个文件名,将它们存储在一个数组中,然后用这些名称创建一个文件.现在,我正在使用fopen和fread,它需要字符数组(我试图优化速度),所以我的文件名被读入一个字符数组数组.但是,这些数组需要预先确定最大大小,因此如果文件名小于最大值,则其余数据充满了垃圾.然后,当我尝试在fopen()中使用该数组作为文件名时,它无法识别该文件,因为它在字符串的末尾有垃圾.我怎么解决这个问题?这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include "stdafx.h"
#define NUM_INPUT_FILES 4
using namespace std;
FILE *fp;
unsigned char *buff;
FILE *inputFiles[NUM_INPUT_FILES];
int _tmain(int argc, _TCHAR* argv[])
{
buff = (unsigned char *) malloc(2048);
char j[8];
char outputstring[] = "Feelings are not supposed to be logical. Dangerous is the man who has rationalized his emotions. (David Borenstein)";
fp = fopen("hello.txt", "r");
string tempfname[NUM_INPUT_FILES];
//fp = fopen("hello.txt", "r");
for(int i=0;i<NUM_INPUT_FILES;i++)
{
fgets(tempfname[i], 20, fp);
cout << tempfname[i];
}
fclose(fp);
for(int i=0; …Run Code Online (Sandbox Code Playgroud)