交作业充数用,嘿嘿

import requests
import re
import xlwt
#from lxml import etree
url0 = 'http://iframe.chinapost.com.cn/jsp/type/institutionalsite/SiteSearchJT.jsp?community=ChinaPostJT&pos={}'
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36",
            "Cookie":"JSESSIONID=XXXXXXXX3592967B7C555562F2"}

book = xlwt.Workbook(encoding='utf-8')
sheet = book.add_sheet('中国邮政网点信息表')

# 0,5306
ii=0
for i in range(0 ,5306 ):
    url = url0.format(str(i*10))
    print(url)
    res = requests.get(url, headers=headers)
    res.encoding = 'utf-8'
    # print(res.text)
    # tree = etree.HTML(res.text.encode('utf-8'))
    # unitname = tree.xpath('/html/body/table/tbody/text()')
    # print(type(unitname), unitname)

    result = re.findall('(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)(.*?)', res.text, re.S)

    for row in result:
        ii = ii+1
        print(ii, row)
        for j in range(0, 7):
            sheet.write(ii -1 ,j ,row[j])

book.save('D:\邮政网点表.xls')

你可能感兴趣的:(交作业充数用,嘿嘿)