- 以某大学学院主页为例:http://homepage.hrbeu.edu.cn/irisweb/manage/resume/search/index-subject
- 代码如下:
import requests
from lxml import etree
from bs4 import BeautifulSoup
import re
import os
def makedir(filePath):
E = os.path.exists(filePath)
if not E:
os.makedirs(filePath)
os.chdir(filePath)
print('文件夹<' + filePath + '>创建成功!')
else:
print('文件夹已存在!')
xueYuanUrl = 'http://homepage.hrbeu.edu.cn/irisweb/manage/resume/search/index-subject'
teacherUrl = 'http://homepage.hrbeu.edu.cn/irisweb/manage/resume/search/ajaxSearchBySubjectNewPage'
teacherDetailUrl = 'http://homepage.hrbeu.edu.cn/web/'
filePath = 'F:/python/teacherInfo/'
webSide = requests.get(xueYuanUrl)
webSide.encoding = webSide.apparent_encoding
selector = etree.HTML(webSide.text)
xueYuanNames = selector.xpath('//*[@id="subject_detail_div"]/ul/li/a/text()')
xueYuanCodes = selector.xpath('//*[@id="subject_detail_div"]/ul/li/a/@code')
xueYuanInfo = dict(zip(xueYuanCodes, xueYuanNames))
for key, value in xueYuanInfo.items():
params = {
'subject_code': key, 'subject_name': value}
xueYuanFilePath = filePath + value + '/'
makedir(xueYuanFilePath)