How can I use pyqt to capture webpage content containing javascript execution results?

Source: Internet
Author: User

The methods mentioned in "capturing webpage content using Python and beautifulsoup" may encounter problems when processing Javascript execution results on the webpage. For example, when I want to capture the online ticket search results, the crawled result is "Please wait, your query result is being searched in real time ". This is not the result I want. I raised this question on stackoverflow. The reply is that the urllib module in Python cannot solve this problem, because the webpage calls the JavaScript function to execute the search and load the search results. The solution provided by this reply is pyqt orSelenium. Because I still want to solve this problem using python, I tried pyqt.

Pyqt is a python library developed for the Nokia QT application framework. It can run on Windows, Mac OSX, and Linux platforms. The latest version is pyqt v4.9.4.

Install pyqt4 on Mac OSX: Install pyqt v4.9.4 on Mac OSX 10.7.5.

1. Download and install QT. Based on InstallationProgramThe wizard can be executed step by step.

2. Download and install sip. SIP is a tool for connecting Python and C/C ++. Unzip the SIP installation package and run:

 
Cd ~ /Downloads/SIP-4.13.3Python3 Configure. py-D/library/Python/3.2/Site-packages --Arch x86_64makesudo make install

-- Arch x86_64 specifies the architecture of the SIP Installation Platform.

3. Download and install pyqt4. Decompress the installation package and execute:

 
CD PyQt-mac-gpl-4.9.4Python3 Configure. py-Q/users/SAM/qtsdk/desktop/QT/4.8.1/GCC/bin/qmake-D/library/Python/3.2/Site-packages/-- use-Arch x86_64makesudo make install

This installation process may take some time. /Users/SAM/qtsdk is the installation directory of QT.

Using qtwebkit to capture javascript execution results on webpages

Qtwebkit provides a Web browser engine that can parse HTML containing CSS and Js. According to stackoverflow's reply, I tried to use qwebpage in qtwebkit to solve my problem, for exampleCodeAs follows:

View code

 Import  Sys  Import  Signal Import  Urllib. parse  From Pyqt4.qtwebkit Import  Qwebpage  Class  Crawler (qwebpage ):  Def   _ Init __  (Self, URL, file): qwebpage.  _ Init __  (Self) self. _ URL = URL self. _ file = File Def  Crawl (Self): signal. Signal (signal. SIGINT, signal. sig_dfl) self. Connect (self, signal (  '  Loadfinished (bool)  '  ), Self. _ finished_loading) self. Mainframe (). Load (qurl (self. _ URL ))  Def  _ Finished_loading (self, result): File = Open (self. _ file, '  W  ' ) File. Write (self. Mainframe (). tohtml () file. Close () SYS. Exit (0)  Def  Main (): app = Qapplication (SYS. argv) URL = '  Http://flight.qunar.com/site/oneway_list.htm  '  Values = { '  Searchdepartureairport  ' : '  Beijing ' , '  Searcharrivalairport  ' : '  Lijiang  ' , '  Searchdeparturetime  ' : '  2012-07-25  '  } Encoded_param = Urllib. parse. urlencode (values) full_url = URL +'  ?  ' + Encoded_param filename = '  Output.txt  '  Crawler = Crawler (full_url, filename) crawler. Crawl () SYS. Exit (app.exe C _())  If   _ Name __ = '  _ Main __  '  : Main () 

 

But unfortunately, what I got is still "Please wait, your query results are being searched in real time ". Maybe something is wrong. Maybe pyqt cannot solve my problem. Maybe pyqt cannot solve it. The problem is still being explored.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.