I want to try to climb down the beiyou forum, but I can see that the source code on the page is JavaScript and there is almost no information I want. I want to try to climb down the beiyou forum, but I can see that the source code on the page is JavaScript and there is almost no information I want. Reply: Today, I accidentally discovered PyV8, which is what you want.
It directly builds a js runtime environment, which means you can directly execute the js Code on the page in python to get the content you need.
Refer:
Http://www.silverna.org/blog? P = 252
Https://code.google.com/p/pyv8/
I directly read the js source code, analyzed it, And then crawled it.
For example, if Ajax is used to request a JSON file, I will first crawl the page, obtain the parameters required by Ajax, directly request the JSON page, then decode the page, process the data, and store the data in the database.
If you directly run all js on the page (as the browser does) and then obtain the final html dom tree, this performance is very bad and it is not recommended to use this method. Because Python and js have poor performance, if this is done, it will consume a lot of CPU resources and ultimately can only achieve extremely low crawling efficiency.
Js Code is run by the js engine.Python can only obtain the original HTML, CSS, and JS Code through HTTP requests.
I do not know whether there is a JS engine written in Python, and it is estimated that there is little demand.
I usually use PhantomJS
CasperJS
These engines are used for browser crawling.
Write JS Code directly for DOM control and analysis, and output results in file mode.
Let Python call the program and get the content by reading the file. Last year, I really climbed this data because my method was ugly in a hurry.
PyQt has a library to simulate browser requests and behaviors (like webkit. If you forget it, just check it. A few lines of code is enough). In a single running program, the first (only the first time) returned result is the code after js running. So I wrote a py script for access parsing, and then wrote a windows Script to loop through the command line parameters, and finally got the data.
The dirty method is a little better, but the data can be obtained ~ For a website, you can view the network request to find the targeted content that is returned. If it is common, you must use headless browser, such as PhantomJS. Another one crawls the beiyou forum ..
Literary Methods: browser engines, such as PhantomJS
, Use it to export html, and then parse the html in python. Do not parse PhantomJS directly. Although I know it is easy, why? That's not a python crawler.Because python is used for unified parsing, we assume that you are still crawling non-JS pages.
The common method is to analyze AJAX requests. Even if it is rendered by JS, data is still transmitted over HTTP. What? Can't you simulate it? Read the HTTP protocol againIn fact, it is still experience. For example, it is required to request the body of the beiyou forum.
X-Requested-With:XMLHttpRequest
Here is an example:
Job list
After clicking Android, we uploaded several parameters from the browser to the server that pulled the check.
One is first = true, the other is kd = android, the other is pn = 1 (page number)
Therefore, we can simulate this step to construct a data packet to simulate the user's click action.
post_data = {'first':'true','kd':'Android','pn':'1'}
Although this was a long time ago problem, it seems that the subject has also solved this problem. However, there are too many ways to answer these questions. Here we will share a method that is more efficient and consumes less resources. Because the subject does not specify what is needed, the example here takes the links and titles of all the posts on the homepage.
First, remember that the browser environment consumes a lot of memory and CPU resources,
Simulate the browser Environment Crawler code should be avoided as much as possible. Remember, for some front-end rendered web pages, even though we do not see the data we need in the HTML source code, but what's bigger is that it will get pure data through another request (probably in JSON format). Instead of simulating the browser, we can save the consumption of parsing HTML.
Then, open the homepage of beiyou forum and find that the HTML source code of its homepage does not contain the content of the article displayed on the page. Therefore, it is likely that this is asynchronously loaded to the page through JS. Through browser development tools (iN OS x, Chrome Uses command + option + I or F12 in Windows/Linux) to analyze requests when loading the home page, it is easy to find the following requests:
The response obtained from the selected request is the article link of the homepage. You can view the rendered preview in the preview option: The response obtained from the selected request is the article link of the homepage. You can view the rendered preview in the preview option:
So far, we are sure that this link can get the articles and links on the homepage. So far, we are sure that this link can get the articles and links on the homepage.
In the headers option, we have the request header and request parameters for this request. We can use Python to simulate this request and get the same response. Parse HTML with libraries such as BeautifulSoup to get the corresponding content.
For details about how to simulate requests and parse HTML, go to my column. I will not go into details here.
In this way, you do not need to simulate the browser environment to capture data, which greatly improves the memory and CPU consumption and capture speed. When writing crawlers, remember not to simulate the browser environment unless necessary. In windows, you can try to call the webbrowser control in windows. In addition, ie also provides interfaces. However, both of these methods require page rendering, which is a waste of performance. to speed up the process, you can disable the download and display of ie images, and then use the click method to simulate real behavior. Google Phantom JS