中国国民经济行业分类及代码下载(含爬取代码)

爬取网站:https://www.shujukuji.cn/guominjingjihangyefenlei/xiaolei-liebiao

数据:(由于文档中含有敏感词,上传失败)

网盘:

链接:https://pan.baidu.com/s/1nsOK3qM63b0VVgHEfdtssg
提取码:jlx7

 数据截图:

代码分析:

如下图,可以发现代码与连接的关系https://www.shujukuji.cn/guominjingjihangyefenlei/xiaolei/...

 ①先爬取<a>标签中的代码

②根据代码创造链接url,爬取行业名称

 注意:python对txt文档的读写规则以及文档指针问题

 

在使用Python进行txt文件的读写时,当打开文件后,首先用read()对文件的内容读取,然后再用write()写入,这时发现虽然是用“r+”模式打开,按道理是应该覆盖的,但是却出现了追加的情况。
这是因为在使用read后,文档的指针已经指向了文本最后,而write写入的时候是以指针为起始,因此就产生了追加的效果。
如果想要覆盖,需要先seek(0),然后使用truncate()清除后,即可实现重新覆盖写入

源代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 国民经济行业分类及其代码.py
# @Author: 田智凯
# @Date  : 2020/3/19
# @Desc  :爬取中国国民经济行业分类

from multiprocessing.pool import Pool
import requests
from lxml import etree
import time

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}
txtpath=r'D:\hy.txt'

#爬取表格得到小类代码
def get_codes(url):
    global codes
    res = requests.get(url, headers=headers)
    selector = etree.HTML(res.text)
    tb=selector.xpath('//*[@id="block-system-main"]/div/div/div[2]/table/tbody/tr')
    #不清空原来内容
    with open(txtpath, "a",encoding='utf-8') as txt:
        for tr in tb:
            tds=tr.xpath('td')
            for td in tds:
                code=td.xpath('div/span/a/text()')[0]
                #print(code)
                hy=get_hangye(code)
                txt.write(hy)
                txt.write('\t')
                txt.write(code)
                txt.write('\n')
    txt.close()

#根据代码得到名称
def get_hangye(code):
    url='https://www.shujukuji.cn/guominjingjihangyefenlei/xiaolei/{}'.format(code)
    #print(url)
    res = requests.get(url, headers=headers)
    selector = etree.HTML(res.text)
    hy = selector.xpath('//*[@id="block-system-main"]/div/div/div[2]/div/div[2]/span[2]/text()')[0]
    #print(hy)
    return hy

if __name__ == '__main__':
    urls = ['https://www.shujukuji.cn/guominjingjihangyefenlei/xiaolei-liebiao?page={}'.format(str(p)) for p in range(0,15)]
    start=time.time()
    print('程序运行中...')
    pool=Pool(processes=4)#开启四个进程
    pool.map(get_codes,urls)
    end=time.time()
    print('程序用时',end-start)

猜你喜欢

转载自www.cnblogs.com/sengzhao666/p/12528305.html