-->

网络刮填写(和检索)搜索的形式?(web scraping to fill out (and ret

2019-06-26 14:28发布

我想知道是否有可能为“自动”项中输入要搜索的形式和提取结果比赛的任务。 举例来说,我有期刊文章的列表,我想获得DOI(数字对象标识符); 手动为了这个,我会去的期刊文章搜索页面(例如, http://pubs.acs.org/search/advanced ),在作者/标题/体积(等),然后键入找到文章出其返回的结果列表,并挑选出DOI并粘贴到我的参考名单。 我使用R和Python的数据分析定期(我被上RCurl后的启发),但不知道很多关于Web协议......这样的事情可能的(例如,使用像Python的BeautifulSoup?)。 是否有做任何远程类似这样的任务,任何有益的参考? 我只是在学习网页抓取和网站一般刮尽可能让这个特殊的任务完成的工具很感兴趣......感谢您的时间!

Answer 1:

美丽的汤是伟大的解析webpages-这就是你想要做什么的一半。 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的建设来解析文档,为您节省一些严重的时间。

祝好运!



Answer 2:

Python代码:用于搜索表单。

# import 
from selenium import webdriver

from selenium.common.exceptions import TimeoutException

from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0

from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# go to the google home page
driver.get("http://www.google.com")

# the page is ajaxy so the title is originally this:
print driver.title

# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")

# type in the search
inputElement.send_keys("cheese!")

# submit the form (although google automatically searches now without submitting)
inputElement.submit()

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))

    # You should see "cheese! - Google Search"
    print driver.title

finally:
    driver.quit()

来源: https://www.seleniumhq.org/docs/03_webdriver.jsp



Answer 3:

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 

(乔Albahri的改编“C#一言以蔽之”)



Answer 4:

有对网页抓取的工具。 这是一个很好的Firefox插件称为iMacros的。 它的伟大工程,并在所有不需要编程知识。 免费版本可以从这里下载: https://addons.mozilla.org/en-US/firefox/addon/imacros-for-firefox/约iMacros的最好的事情是,它可以让你在几分钟内启动,它也可以从bash命令行启动,也可以从的bash脚本中调用。

更先进的步骤将是硒webdrive。 我选择了硒的原因是,它是在一个伟大的方式相适应初学者记录。 阅读只是以下页面:

会让你upand在任何时间运行。 硒支持使用Java,Python,PHP,C所以,如果你熟悉任何一种语言,你会熟悉的所有命令。 我喜欢硒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();
    }}

我希望这可以给你一个良好的开端。

干杯:)



文章来源: web scraping to fill out (and retrieve) search forms?