Python crawls dynamically generated Web pages--take Baidu mobile assistant as an example

Source: Internet
Author: User

When crawling the page of JS dynamically generated, directly open the page is not get content, for example, I crawl Baidu mobile phone assistant application, encountered such a problem. When searching for a travel application, the return data has several pages of content, but no matter how many pages you turn to, the source code is the source of the first page. Analysis reasons I think it might be this way: Assuming that Baidu application page content has eight applications, you submit the query after the first 8 of the content to generate an HTML, and then you page, through Js,ajax and other ways to replace the original 8 applications, such as when you choose the Fifth page, Replace the 33-40 apps that return the app list with the original 1-8 content. (It should be more likely that Ajax or other forms will be submitted, because pure JS does not involve background interaction such as form submissions, it needs to return all results to the front end at the first query) "Personal analysis, Welcome to correct."

Article ideas from: http://www.3fwork.com/b401/000595MYM019975/thank the original author

OK, now we have a clear goal, to find the corresponding expression of the submission (or JS) action can be. Here we use Google's developer tools. The next thing we do is in Google Chrome.

Let's do it from the ground up:

1, open the mobile phone assistant search "Travel", url:http://shouji.baidu.com/s?wd= travel

We get a search result:for you to find:Travelof theApplicationResults461a


At this point you can view the source files.

2, when we turn a page url:http://shouji.baidu.com/s?wd= travel #page2 found the source file is the same, this is caused by dynamic loading, then how do we grasp the back of the interface?

3. Open Google Developer tools:



There is no content at this time, we need to load page: Url:http://shouji.baidu.com/s?wd=%e6%97%85%e6%b8%b8#page2

While maintaining the developer tools, the address bar enters the URL of the page you want to parse (such as the URL above), and then you can see the content:



There are so many things in it that we need to look at each other, but you will find that there are not many things that are really valuable. We finally found this:



The app name is the app name that we want to crawl to get the second page, and this is what we want to end up with.

4, on the left side of the red box right-click, you can copy the link: http://shouji.baidu.com/s?data_type=app&multi=0&ajax=1&wd=%E6%97%85%E6%B8%B8 &page=1&_=1460652637109

Open this link we found that the source code of this page is what we want (in fact, the submission of the form) this is not very nice!

5, but this still does not, we want is a general method, this form submits after that figure how to construct, here directly delete is good, namely http://shouji.baidu.com/s?data_type=app&multi=0 &ajax=1&wd=%e6%97%85%e6%b8%b8&page=1 Open after the effect of the same, if some sites this is influential, you can catch a few more rules.

6, through our current information, summed up the law:

Http://shouji.baidu.com/s?data_type=app&multi=0&ajax=1&wd=app_name&page= page

#关于页码, you need to explain, if it is page N, page number to fill n-1, the first page is 0

7, there may be garbled, need to change the code of Chrome to UTF8

Python source for this crawler:

#encoding: UTF8# @Author Lvpengbin" "crawl Baidu Mobile assistant App_name Sample code to crawl search keyword "Travel" returns the second page of results for example url:http://shouji.baidu.com/s?wd= travel #page2" "ImportSysReload(SYS) Sys.setdefaultencoding ("Utf-8")ImportUrllib2 fromBs4ImportBeautifulsoupurl=' http://shouji.baidu.com/s?data_type=app&multi=0&ajax=1&wd=%E6%97%85%E6%B8%B8&page=1 ' Req_header = {' User-agent ':' mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/38.0.2125.122 safari/537.36 SE 2.X METASR 1.0 ',' Accept ':' */* ',' Accept-language ':' zh-cn,zh;q=0.8 ',' Connection ':' Close ',' Referer ':None#注意如果依然不能抓取的话, you can set the host of the crawl site here}#这里需要设置header, otherwise the system default Accept-language is English, the returned page is in EnglishReq_timeout =5req = Urllib2. Request (URL,None, req_header) resp = Urllib2.urlopen (req,None, req_timeout) HTML = resp.read () soup = beautifulsoup (html) nodes = Soup.find_all ("a",onclick="BD_APP_DL (this,event);") forNodeinchNodesPrintnode[' Data_name ']" "Output:Uber -Stepma Cellular free lineflying is always accurateTripAdvisorand Lifeeasy to use carLove the cityExpress train Ticket-12306 ticketTripAdvisorAmoy on the road tour" "



Python crawls dynamically generated Web pages--take Baidu mobile assistant as an example

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.