헤드리스 인터넷 브라우저?
다음을하고 싶습니다. 웹 사이트에 로그인하고 몇 가지 특정 링크를 클릭 한 다음 다운로드 링크를 클릭합니다. Windows에서 예약 된 작업으로 실행하거나 Linux에서 cron 작업으로 실행하고 싶습니다. 나는 내가 사용하는 언어에 대해 까다 롭지 않지만 가능하면 브라우저 창을 화면에 올리지 않고 실행하고 싶습니다.
내가 알고있는 헤드리스 브라우저 목록은 다음과 같습니다.
- HtmlUnit- 자바. 맞춤형 브라우저 엔진. 제한된 JavaScript 지원 / DOM 에뮬레이션. 오픈 소스.
- Ghost -Python 전용입니다. WebKit 기반. 완전한 JavaScript 지원. 오픈 소스.
- 능직 -Python / 명령 줄. 맞춤형 브라우저 엔진. 자바 스크립트가 없습니다. 오픈 소스.
- PhantomJS- 명령 줄 / 모든 플랫폼. WebKit 기반. 완전한 JavaScript 지원. 오픈 소스.
- Awesomium -C ++ /. NET / 모든 플랫폼. 크롬 기반. 완전한 JavaScript 지원. 상업용 / 무료.
- SimpleBrowser-.NET 4 / C #. 맞춤형 브라우저 엔진. JavaScript 지원이 없습니다. 오픈 소스.
- ZombieJS -Node.js. 맞춤형 브라우저 엔진. 자바 스크립트 지원 / 에뮬레이트 된 DOM. 오픈 소스. jsdom 기반 .
- EnvJS -Java / Rhino를 통한 JavaScript. 맞춤형 브라우저 엔진. 자바 스크립트 지원 / 에뮬레이트 된 DOM. 오픈 소스.
- Watir과 - webdriver 와 헤드리스 보석 - WebDriver를 통해 루비. 브라우저를 통한 완전한 JS 지원 (Firefox / Chrome / Safari / IE).
- Spynner -Python 전용입니다. PyQT 및 WebKit.
- jsdom -Node.js. 맞춤형 브라우저 엔진. 에뮬레이트 된 DOM을 통해 JS를 지원합니다. 오픈 소스.
- TrifleJS -MSIE (Trident) 및 V8을 사용하는 PhantomJS 포트. 오픈 소스.
- ui4j- 순수 Java 8 솔루션. JavaFx WebKit Engine을 포함하는 래퍼 라이브러리. 헤드리스 모드.
- Chromium 임베디드 프레임 워크 -필요에 따라 오프 스크린 렌더링을 사용하는 Chromium의 최신 임베디드 버전입니다. .NET 래퍼 (및 기타 언어)가있는 C / C ++ Chromium이므로 모든 것을 지원합니다 . BSD 라이센스.
- Selenium WebDriver- 브라우저 (Firefox, IE, Chrome, Safari, Opera)를 통해 JavaScript를 완벽하게 지원합니다. 공식적으로 지원되는 바인딩 은 C #, Java, JavaScript, Haskell, Perl, Ruby, PHP, Python, Objective-C 및 R입니다. 비공식 바인딩은 Qt 및 Go에 사용할 수 있습니다 . 오픈 소스.
에뮬레이트 된 DOM을 통해 JavaScript를 지원하는 헤드리스 브라우저는 일반적으로 고급 / 모호한 브라우저 기능을 사용하거나 시각적 종속성이있는 기능 (예 : CSS 위치 등)이있는 일부 사이트에 문제가 있습니다. 브라우저는 일반적으로 완전하며 실제 지원되는 브라우저 기능은 부분적인 것으로 간주되어야합니다.
(참고 :이 게시물의 원본 버전은 HtmlUnit 만 언급했기 때문에 주석입니다. 다른 헤드리스 브라우저 구현에 대해 알고 있고 편집 권한이있는 경우이 게시물을 편집하고 추가해도됩니다.)
찾고있는 내용에 대한 매우 편리한 스크립팅 언어 인 twill을 확인하십시오 . 로부터 예 :
setlocal username <your username>
setlocal password <your password>
go http://www.slashdot.org/
formvalue 1 unickname $username
formvalue 1 upasswd $password
submit
code 200 # make sure form submission is correct!
더 많은 유연성을 찾고 있다면 Python API 도 있습니다 .
Windows, Mac OS X, Linux 및 기타 * ix 시스템에서 사용할 수 있는 JavaScript 기반 자동화 프레임 워크 인 PhantomJS를 살펴보십시오 .
PhantomJS를 사용하면 다음과 같은 작업을 수행 할 수 있습니다.
console.log('Loading a web page');
var page = new WebPage();
var url = "http://www.phantomjs.org/";
page.open(url, function (status) {
// perform your task once the page is ready ...
phantom.exit();
});
또는 페이지 제목 평가 :
var page = require('webpage').create();
page.open(url, function (status) {
var title = page.evaluate(function () {
return document.title;
});
console.log('Page title is ' + title);
});
PhantomJS의 빠른 시작 페이지의 예 . render () 메서드를 사용하여 페이지를 PNG, JPEG 또는 PDF로 렌더링 할 수도 있습니다 .
Internet Explorer ActiveX 컨트롤 (WebBrowser, MSHTML)을 사용하여 한 적이 있습니다. 표시하지 않고 인스턴스화 할 수 있습니다.
COM (Delphi, VB6, VB.net, C #, C ++, ...)을 지원하는 모든 언어로이 작업을 수행 할 수 있습니다.
Of course this is a quick-and-dirty solution and might not be appropriate in your situation.
PhantomJS is a headless WebKit-based browser that you can script with JavaScript.
Except for the auto-download of the file (as that is a dialog box) a win form with the embedded webcontrol will do this.
You could look at Watin and Watin Recorder. They may help with C# code that can login to your website, navigate to a URL and possibly even help automate the file download.
YMMV though.
If the links are known (e.g, you don't have to search the page for them), then you can probably use wget
. I believe that it will do the state management across multiple fetches.
If you are a little more enterprising, then I would delve into the new goodies in Python 3.0. They redid the interface to their HTTP stack and, IMHO, have a very nice interface that is susceptible to this type of scripting.
Node.js with YUI on the server. Check out this video: http://www.yuiblog.com/blog/2010/09/29/video-glass-node/
The guy in this video Dav Glass shows an example of how he uses node to fetch a page from Digg. He then attached YUI to the DOM he grabbed and can completely manipulate it.
If you use PHP - try http://mink.behat.org/
You can use Watir with Ruby or Watin with mono.
Also you can use Live Http Headers (Firefox extension) to record headers which are sent to site (Login -> Links -> Download Link) and then replicate them with php using fsockopen. Only thing which you'll probably need to variate is the cookie's value which you receive from login page.
libCURL could be used to create something like this.
Can you not just use a download manager?
There's better ones, but FlashGet has browser-integration, and supports authentication. You can login, click a bunch of links and queue them up and schedule the download.
You could write something that, say, acts as a proxy which catches specific links and queues them for later download, or a Javascript bookmarklet that modifies links to go to "http://localhost:1234/download_queuer?url=" + $link.href
and have that queue the downloads - but you'd be reinventing the download-manager-wheel, and with authentication it can be more complicated..
Or, if you want the "login, click links" bit to be automated also - look into screen-scraping.. Basically you load the page via a HTTP library, find the download links and download them..
Slightly simplified example, using Python:
import urllib
from BeautifulSoup import BeautifulSoup
src = urllib.urlopen("http://%s:%s@example.com" % ("username", "password"))
soup = BeautifulSoup(src)
for link_tag in soup.findAll("a"):
link = link_tag["href"]
filename = link.split("/")[-1] # get everything after last /
urllib.urlretrieve(link, filename)
That would download every link on example.com, after authenticating with the username/password of "username" and "password". You could, of course, find more specific links using BeautifulSoup's HTML selector's (for example, you could find all links with the class "download", or URL's that start with http://cdn.example.com
).
You could do the same in pretty much any language..
.NET contains System.Windows.Forms.WebBrowser. You can create an instance of this, send it to a URL, and then easily parse the html on that page. You could then follow any links you found, etc.
I have worked with this object only minimally, so I'm no expert, but if you're already familiar with .NET then it would probably be worth looking into.
참고URL : https://stackoverflow.com/questions/814757/headless-internet-browser
'developer tip' 카테고리의 다른 글
Bash에서 읽기 전용 변수 설정 해제 (0) | 2020.11.07 |
---|---|
Java의 enum에서 문자열 값 가져 오기 (0) | 2020.11.07 |
알 수없는 수의 인수를 자바 스크립트 함수에 전달 (0) | 2020.11.07 |
와일드 카드를 사용하여 PostgreSQL에서 여러 테이블을 삭제하는 방법 (0) | 2020.11.07 |
'data : font / woff…'글꼴로드를 거부하면 다음 콘텐츠 보안 정책 지침을 위반합니다.“default-src 'self'”. (0) | 2020.11.06 |