2009-07-23 66 views
5

我想知道是否可以「自動化」輸入條目來搜索表單並從結果中提取匹配的任務。例如,我有一份期刊文章列表,我希望獲得DOI(數字對象標識符);手動爲此我會去期刊文章搜索頁面(例如,http://pubs.acs.org/search/advanced),鍵入作者/標題/卷(等),然後從返回的結果列表中找到文章,然後挑出DOI並粘貼那到我的參考名單。我經常使用R和Python進行數據分析(我受到了RCurl的一篇文章的啓發),但對網絡協議知之甚少......是否有這種可能(例如使用Python的BeautifulSoup之類的東西?)。有沒有什麼好的參考資料可以做類似於這個任務的任何事情?我同樣對學習關於網絡抓取和一般網絡抓取工具以及完成此特定任務感興趣......感謝您的時間!網絡抓取填寫(和檢索)搜索表單?

+0

你有沒有想出一個很好的解決這個問題?我在這裏問了一個類似的(重複?)問題後發現這個問題http://stackoverflow.com/questions/9711539/can-i-query-the-digital-object-identifier-for-a-list-of-citations – 2012-03-14 23:16:09

+0

@大衛 - 不,不好意思。沒有足夠的選擇來評論... – hatmatrix 2012-03-24 00:51:15

回答

9

美麗的湯是偉大的解析webpages-是(從喬Albahri的「C#一言以蔽之」改編)是的,你想做什麼的一半。 Python和Perl和Ruby都有一個版本機械化的,那就是另一半:

http://wwwsearch.sourceforge.net/mechanize/

機械化讓你控制瀏覽器:

# Follow a link 
browser.follow_link(link_node) 

# Submit a form 
browser.select_form(name="search") 
browser["authors"] = ["author #1", "author #2"] 
browser["volume"] = "any" 
search_response = br.submit() 

隨着機械化和美麗的湯你有偉大的開始。一個額外的工具,我會考慮的是Firebug的,因爲這快速紅寶石刮指南中使用:

http://www.igvita.com/2007/02/04/ruby-screen-scraper-in-60-seconds/

螢火蟲可以加快您來解析文檔,爲您節省一些嚴重的時間的XPath的建設。

祝你好運!

+0

偉大的!謝謝 - 非常有幫助! – hatmatrix 2009-07-23 19:40:58

1
WebRequest req = WebRequest.Create("http://www.URLacceptingPOSTparams.com"); 

req.Proxy = null; 
req.Method = "POST"; 
req.ContentType = "application/x-www-form-urlencoded"; 

// 
// add POST data 
string reqString = "searchtextbox=webclient&searchmode=simple&OtherParam=???"; 
byte[] reqData = Encoding.UTF8.GetBytes (reqString); 
req.ContentLength = reqData.Length; 
// 
// send request 
using (Stream reqStream = req.GetRequestStream()) 
    reqStream.Write (reqData, 0, reqData.Length); 

string response; 
// 
// retrieve response 
using (WebResponse res = req.GetResponse()) 
using (Stream resSteam = res.GetResponseStream()) 
using (StreamReader sr = new StreamReader (resSteam)) 
    response = sr.ReadToEnd(); 

// use a regular expression to break apart response 
// OR you could load the HTML response page as a DOM 

+0

謝謝 - 很高興知道這是可能的! ...我在猜測。 (不太熟悉.NET,雖然我聽到它是所有的憤怒...) – hatmatrix 2009-07-23 19:42:08

0

有很多網頁抓取工具。有一個很好的叫做iMacros的firefox插件。它效果很好,完全不需要編程知識。免費版可以從這裏下載: https://addons.mozilla.org/en-US/firefox/addon/imacros-for-firefox/ 關於iMacros的最好的事情是,它可以讓你在幾分鐘內開始,它也可以從bash命令行啓動,也可以從bash腳本中調用。

更先進的步驟將是硒webdrive。我選擇硒的原因在於它適合初學者,是一種很好的方法。閱讀下面的內容page:

會讓你立即運行。 Selenium支持java,python,php,c,所以如果你熟悉這些語言,你會熟悉所有需要的命令。我更喜歡sed的webdrive變體,因爲它會打開瀏覽器,以便您可以檢查字段和輸出。使用webdrive設置腳本後,您可以輕鬆將腳本遷移到IDE,從而無需運行。

通過鍵入命令

sudo easy_install selenium 

這將需要你的依賴和照料一切安裝硒可以做。

爲了交互運行腳本,只需打開一個終端,並鍵入

python 

你會看到Python提示符,>>>你可以在命令輸入。

這裏,你可以在終端粘貼示例代碼,它將谷歌搜索詞奶酪

package org.openqa.selenium.example; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.ExpectedCondition; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class Selenium2Example { 
    public static void main(String[] args) { 
     // Create a new instance of the Firefox driver 
     // Notice that the remainder of the code relies on the interface, 
     // not the implementation. 
     WebDriver driver = new FirefoxDriver(); 

     // And now use this to visit Google 
     driver.get("http://www.google.com"); 
     // Alternatively the same thing can be done like this 
     // driver.navigate().to("http://www.google.com"); 

     // Find the text input element by its name 
     WebElement element = driver.findElement(By.name("q")); 

     // Enter something to search for 
     element.sendKeys("Cheese!"); 

     // Now submit the form. WebDriver will find the form for us from the element 
     element.submit(); 

     // Check the title of the page 
     System.out.println("Page title is: " + driver.getTitle()); 

     // Google's search is rendered dynamically with JavaScript. 
     // Wait for the page to load, timeout after 10 seconds 
     (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 
      public Boolean apply(WebDriver d) { 
       return d.getTitle().toLowerCase().startsWith("cheese!"); 
      } 
     }); 

     // Should see: "cheese! - Google Search" 
     System.out.println("Page title is: " + driver.getTitle()); 

     //Close the browser 
     driver.quit(); 
    }} 

我希望這可以給你一個良好的開端。

乾杯:)