Python Crawler Implementation Simple example (crawl a page) __python

Source: Internet
Author: User

The simplest implementation of the Python crawler

#!/usr/bin/env python
#coding =utf-8
Import Urllib
Import Urllib2
def Login ():
url = ' Https://www.oschina.net/action/user/hash_login '
values= {' usermail ': ' 123@qq.com ', ' userpassword ': ' 123456 '}
headers = {' user-agent ': ' mozilla/5.0 ' (Macintosh; Intel Mac OS x10_11_0) applewebkit/537.36 (khtml, like Gecko) chrome/56.0.2924.87safari/537.36 '}
data = Urllib.urlencode (values)
Request = Urllib2. Request (Url=url,data=data,headers=headers)
Response = Urllib2.urlopen (Request)
printresponse.read ()
if __name__ = = "__main__":
Login ()

Urllib.urlencode to format the input data, Urllib2.urlopen can be either a request object or a URL string. However, it is recommended that the request object be used because it looks clearer, and the incoming parameters from the build request are clear. Finally, the content of the crawled Web page can be exported.

Note: In the above program added header information, not all sites need to add, but some sites prohibit reptiles to crawl this information, so add this information we can forge into a browser request for access.

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.