English 中文(简体)
谁是谁?
原标题:iPhone - possible to query Wikipedia to see if web page exists?

我很奇怪的是,如果能够看到存在一个百基百页,我就有一个习惯的搜索,把搜索空间替换为——但我看不出这条道路是否实际存在。

    targetWiki = inputCustomTarget.text;
    targetWiki = [targetWiki stringByReplacingOccurrencesOfString:@" " withString:@"_"];
    targetWiki = [NSString stringWithFormat:@"http://en.m.wikipedia.org/wiki/%@", targetWiki];    

如果存在一页,我是否必须把答复同起来?

最佳回答

无需对结果进行核对,以便:https://stackoverflow.com/questions/917932/retrieve-httprequest-status-codes-iphone-sdk>200 回复代码载于<代码>-(避免)联接:NSURLConnection*) 如果不存在,就应当获得404份。

<><>Edit>:

我愿补充说,<>Main上不存在的网页。 Wikipedia页面(不是移动电话)确实退回了正确的404个错误代码。 这可能会改变未来,如果改变任何内容,但两者都不会贬低内容,则可能完全可靠。 这里是我汇集的样本,以证明这一点。

NSURLRequest *exists = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Qwerty"]];
//Redirects to Blivet
NSURLRequest *redirects = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Poiuyt"]];
NSURLRequest *nonexistant = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://en.wikipedia.org/wiki/Jklfdsa"]];

NSHTTPURLResponse *resp_exists;
NSHTTPURLResponse *resp_redirects;
NSHTTPURLResponse *resp_nonexistant;

[NSURLConnection sendSynchronousRequest:exists returningResponse:&resp_exists error:NULL];
[NSURLConnection sendSynchronousRequest:redirects returningResponse:&resp_redirects error:NULL];
[NSURLConnection sendSynchronousRequest:nonexistant returningResponse:&resp_nonexistant error:NULL];

NSLog(@"
Exists: %d
Redirects: %d
Non Existant: %d", 
      [resp_exists statusCode], 
      [resp_redirects statusCode], 
      [resp_nonexistant statusCode] );

And here is the output

Exists: 200
Redirects: 200
Non Existant: 404

So if a page exists or automatically redirects to a page that does exist you will get a 200 error code, if it does not exist then you will get 404. If you would like to capture the redirect you will need implement -connection:willSendRequest:redirectResponse: and act accordingly.

Note: This example code is synchronous for the sake of being compact. This is not ideal and production implementations should be sending asynchronous request and use the NSURLConectionDelegate methods.

问题回答

You can t check the response code because it will always return a 200 response code.

I think the best way to see if a page exists is to parse the response and check if you land on the default search results page.

Another option would be to make use of MediaWiki s API.

http://en.wikipedia.org/w/api.php?action=opensearch&search=term

Check if the term that was searched for exists in the returned response.

是的,恐怕你很可能需要把结果同起来,以知道该页是否存在。 然而,如果你看一看在此提供的全部英文维基百科式垃圾填埋场档案,则可能有一个替代办法;

http://en.wikipedia.org/wiki/Wikipedia:Database_download#Latest_complete_dump_of_English_Wikipedia

Obviously this raw data is huge, but you could write a parser to find all the valid links and then compress that information into (say) a coreData database which you might find could fit on the iPhone. Then you could run a check without having to test the page.

But to be honest, I d probably parse the page and perhaps cache the answer so I only have to do it once.

EDIT: I m afraid the answer given by Joe is not fully correct. When I use the domain that the original question used (ie en.m.wikipedia.org) then Joe s sample code gives the following output.

Exists: 200
Redirects: 200
Non Existant: 200

If I use en.wikipedia.org then my results concur with Joe, however that was not the question asked. I am based in the UK and that might also have a bearing on the results.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签