Python pyquery Learning

Source: Internet
Author: User

Pyquery is a python Implementation of jquery and can be used to parse HTML webpage content. Documents: http://pythonhosted.org/pyquery/

Download: https://pypi.python.org/pypi/pyquery#downloads

The function is tested as follows:

Take http://www.verycd.com/topics/2960375/as an example:

Extract the image link address of originlink:

   1: #!/usr/bin/python                                                                                                        
   2: from pyquery import PyQuery as pyq 
   3:  
   4: doc=pyq(url='http://www.verycd.com/topics/2960375/')
   5: for item in doc('span.post_origin_img'):
   6:   print doc(item).attr('originlink')

Execution result:

Note 1:Because there is a class = "post_origin_img" in the span label, you can locate the label through span. post_origin_img. Then the image corresponds to originlink, so you can obtain the value of originlink for the corresponding item.

NOTE 2:The originlink used here is not originlink because during testing,

   1: for item in doc('span.post_origin_img'):
   2:   print doc(item)                                                                                                        
   3:   print doc(item).attr('originlink')
Test results: The priginlink is in lower case. The reason is unknown.

1: A title = "pdf [04:33:25] Resource update a total of 1 file 42.2 MB" href = "/topics/2960275/" onclick = "verycd. track ('/STAT/topicshot /'); "Target =" _ blank ">" Jin Yong-"HD text version [PDF] </a> </DD>
   2:                                                                                             <dd class="itshot">
3: <a id = "entry_link_2960032" Title = "pdf [05:18:31] A total of 882.31kb" href = "/topics/2960032/" onclick = "verycd. track ('/STAT/topicshot/'); "target =" _ blank "style =" text-Decoration: none; "> 
   4:                         <div id="entry_2960032" style="display:none;" class="entry_score_small"></div>
   5:                         </a>
   6:                         <br />
7: <a Title = "pdf [05:18:31] A total of 882.31kb" href = "/topics/2960032/" onclick = "verycd. track ('/STAT/topicshot/'); "target =" _ blank "> text version of soul 21 grams [PDF] </a> </DD>
   8:                                                                                             <dd class="itshot">
9: <a id = "entry_link_2861322" Title = "Epub [02:06:05] A total of 47 file 1.35 GB" href = "/topics/2861322/" onclick = "verycd. track ('/STAT/topicshot/'); "target =" _ blank "style =" text-Decoration: none; "> 
  10:                         <div id="entry_2861322" style="display:none;" class="entry_score_small"></div>
  11:                         </a>

 

You can extract content by ID and use the # ID name: the instance is as follows:

 

   1: #!/usr/bin/python
   2: from pyquery import PyQuery as pyq 
   3:  
   4: doc=pyq(url='http://www.verycd.com/topics/2960375/')
   5: for item in doc('#entry_link_2861322'):
   6:   print doc(item)

Execution result: HTML with the ID of entry_link_2861322 is extracted.

Self-built test

   1: doc=pyq('<;p id="test" class="test1" attr1="attr1_value">hello</p>')
   2: print doc('p')
   3: print doc('p#test')                                                                                                      
   4: print doc('p#test.test1')
   5: print doc('p#test.test1').attr('attr1')

Execution result:

 

In the above test, we can see that using pyquery, we can specify the HTML tag for extraction, which is very convenient.

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.