Python crawling micro-Letter article method

Source: Internet
Author: User
This article to you to share is the use of Python through the Sogou entrance, crawl the article of the small program, very simple and practical, the need for small partners can refer to the next

I want to get a collection of the article site, helpless really from the students can not find the entrance link, the Internet turned to see a lot of information, found that everyone's approach is similar, are to Sogou for the entrance. The following is the author of a Python crawl article of the code, interested in the Welcome to read


#coding: Utf-8author = ' haoning ' **#!/usr/bin/env pythonimport timeimport datetimeimport requests**import jsonimport Sysreload (SYS) sys.setdefaultencoding ("Utf-8") import reimport xml.etree.ElementTree as Etimport Os#openid = ' Oiwsftyel13zmva1qltq3pfejlwu ' OPENID = ' oiwsftw_-w2dahwrz1ogwzl-wf9m&ext ' xml_list = []# get current time in Millisecondscurrent_milli_time = Lambda:int (Round (Time.time () *)) def Get_json (pageIndex): Global openidthe_ headers = {' user-agent ': ' mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) applewebkit/537.36 (khtml, like Gecko) chrome/39.0.2171.95 safari/537.36 ', ' Referer ': ' Http://weixi n.sogou.com/gzh?openid={0} '. Format (OpenID), ' Host ': ' weixin.sogou.com '}url = ' http://weixin.sogou.com/gzhjs?cb= SOGOU.WEIXIN.GZHCB&OPENID={0}&PAGE={1}&T={2} '. Format (OpenID, PageIndex, Current_milli_time ()) # Urlprint (URL) response = Requests.get (URL, headers = the_headers) # to-do; Check if match the Regresponse_text = response.textprint Response_textjson_start = response_text.index (' Sogou.weixin.gzhcb (') + 19json_end = Response_text.index (') ')-2json_str = Response_text[json_start:json _end] #get Json#print (json_str) # convert JSON_STR to JSON objectjson_obj = Json.loads (json_str) #get JSON obj# print Json_ obj[' totalpages ']return json_objdef add_xml (jsonobj): Global xml_listxmls = jsonobj[' items '] #get item#print type (xmls) Xml_list.extend (xmls) #用新列表扩展原来的列表 **[#www. oksousou.com][2]**#------------Main----------------print ' Play it:) ' # get Total Pagesdefault_json_obj = Get_json (1) total_pages = 0total_items = 0if (default_json_obj): # Add the default Xmlsa Dd_xml (default_json_obj) # get the rest itemstotal_pages = default_json_obj[' totalpages ']total_items = default_json_obj    [' TotalItems ']print total_pages# iterate all Pagesif (Total_pages >= 2): For PageIndex in range (2, Total_pages + 1): Add_xml (Get_json (pageIndex)) #extend print ' load page ' + str (pageIndex) print len (xml_list)

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.