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.