#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2021/3/8 15:49
Project:python FileName:20210312.py
Comment:
'''
import requests
import unittest, time, re,sys, io, os, csv, random
from lxml import etree
from selenium import webdriver
class Test():
def __init__(self):
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='gb18030')
self.base_url = "https://www.exam100.net/index.php?m=search&c=index&a=init&typeid=1&siteid=1&q=02118"
self.proxies = {"http": None, "https": None}
self.headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36"
}
def read_txt(self, txt_path="./province/", txt_name="全国省份市区"):
fp = open(txt_path + txt_name, mode="r+")
all_list = fp.readlines()
fp.close()
return all_list
def wait_time(self, time_secend, comment="等待时间"):
from tqdm import tqdm
import time
pbar = tqdm([wait_second for wait_second in range(int(time_secend)+1)][1:])
for char in pbar:
time.sleep(1)
pbar.set_description("----{} {:<2}".format(comment, char))
def mkdir_file(self, path):
path = path.strip()
path = path.rstrip("\\")
isExists=os.path.exists(path)
if not isExists:
os.makedirs(path)
return True
else:
return False
def write_csv(self, csv_path="./test/", csv_name="top250.csv", mode="a+", klist=[]):
fp = open(csv_path + csv_name, mode=mode, newline='', encoding='utf-8-sig')
writer = csv.writer(fp)
writer.writerow((klist))
fp.close()
def down_picture(self, pic_path, pic_name, pic_url):
try:
res_pic = requests.get(url=pic_url, headers=self.headers, timeout=10, proxies=self.proxies) #二进制数据
# print('【成功】正在下载第图片,图片地址:' + str(pic_url))
self.mkdir_file(pic_path)
pic_dir = pic_path + "/" + pic_name + '.jpg'
fp = open(pic_dir, 'wb+') #给每张图片指定路径并命名
fp.write(res_pic.content) #将图片二进制数据写成图片
fp.close()
return pic_name + ".jpg"
except requests.exceptions.ConnectionError:
print('【失败】当前图片无法下载,图片地址:' + str(pic_url))
def get_info(self, html, mode, x_xpath1=None, x_xpath2=None):
try:
str_result = x_xpath1
if mode == "string":
try:
res_value = html.xpath(str_result)[0].xpath('string(.)').encode('gbk', 'ignore').decode('gbk')
except Exception as e:
print('路径1', str(str_result) + "\t错误码:" + str(e))
try:
res_value = html.xpath(x_xpath2)[0].xpath('string(.)').encode('gbk', 'ignore').decode('gbk')
except Exception as e:
print('路径2', str(x_xpath2) + "\t错误码:" + str(e))
res_value = res_value.encode('gbk', 'ignore').decode('gbk').strip().replace(" ", '').replace("\n", '')
return res_value
elif mode == "url":
try:
res_value = html.xpath(str_result)[0]
except Exception as e:
print('路径1', str(str_result) + "\t错误码:" + str(e))
try:
res_value = html.xpath(x_xpath2)[0]
except Exception as e:
print('路径2', str(x_xpath2) + "\t错误码:" + str(e))
return res_value
elif mode == "list":
try:
res_value = html.xpath(str_result)
except Exception as e:
print('路径1', str(str_result) + "\t错误码:" + str(e))
try:
res_value = html.xpath(x_xpath2)
except Exception as e:
print('路径2', str(x_xpath2) + "\t错误码:" + str(e))
res_value = "".join([l for l in res_value if "\n" not in l])
return res_value
elif mode == 'picture':
try:
res_value = html.xpath(str_result)[0]
except Exception as e:
print('路径1', str(str_result) + "\t错误码:" + str(e))
try:
res_value = html.xpath(x_xpath2)[0]
except Exception as e:
print('路径2', str(x_xpath2) + "\t错误码:" + str(e))
return res_value
# if isinstance(res_value, str):
# res_value = res_value.encode('gbk', 'ignore').decode('gbk').strip().replace(" ", '').replace("\n", '')
# elif isinstance(res_value, list):
# res_value = "".join([l for l in res_value if "\n" not in l])
except Exception as e:
# res_value = str(str_result) + "\t错误码:" + str(e)
res_value = ".."
print('路径3', str(str_result) + "\t错误码:" + str(e))
print('文件3', e.__traceback__.tb_frame.f_globals['__file__'])
print('行号3', e.__traceback__.tb_lineno)
return res_value
def find_file_name(self, file_dir):
li = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.html':
li.append(os.path.join(root, file))
return li
def get_response(self, url):
response = requests.get(url=url, headers=self.headers)
return response
def get_response_html(self, path_name, content):
with open(path_name, 'wb') as f:
f.write(content)
def get_selenium_html(self, basic_url="https://www.exam100.net/index.php?m=search&c=index&a=init&typeid=1&siteid=1&q=02118"): # 02118
self.driver = webdriver.Chrome()
self.driver.implicitly_wait(10)
self.driver.maximize_window()
driver = self.driver
driver.get(url=basic_url)
cur_title = driver.title.replace(" ", "")
print(f"Title:{cur_title}")
res_html = driver.find_element_by_xpath("//*").get_attribute("outerHTML")
real_name = cur_title.split('-')[-1] + "_信息检索"
self.mkdir_file(f"./selenium/{real_name}/html")
file_name = f"./selenium/{real_name}/html/{real_name}.html"
with open(file_name, "wb") as f:
f.write(res_html.encode())
self.wait_time(random.randint(1, 3), comment=real_name)
return res_html
def get_catalog(self, file_name=None):
with open(file_name, "r", encoding="utf-8") as f:
all_read = f.read().encode('gbk', 'ignore').decode('gbk')
html = etree.HTML(all_read, etree.HTMLParser())
main_xpath = '''//span[contains(text(),'检索结果')]/../../following-sibling::div[1]/ul/li'''
html_list = html.xpath(main_xpath)
print(f"\n{file_name}:" + str(len(html_list)) + "条数据")
for i in range(1, len(html_list)+1, 1):
c_title = self.get_info(html=html, mode="list", x_xpath1=main_xpath + f'[{i}]//span//text()')
c_date = self.get_info(html=html, mode="string", x_xpath1=main_xpath + f'[{i}]//i')
c_href = self.get_info(html=html, mode="url", x_xpath1=main_xpath + f'[{i}]//a/@href')
print(end=f'\n------------------------\n')
print(c_title, end='\n')
print(c_date, end='\n')
print(c_href, end='\n')
res = self.get_response(c_href)
pic_html = etree.HTML(res.content, etree.HTMLParser())
pic_xpath = '''//div[@style="text-align: center;"]/img'''
pic_list = pic_html.xpath(pic_xpath)
print(f"\n{file_name}_:" + str(len(pic_list)) + "条数据")
p_title = self.get_info(html=pic_html, mode="list", x_xpath1="//title//text()").replace(" ", "").split("-")[0]
for j in range(1, len(pic_list)+1, 1):
p_href = self.get_info(html=pic_html, mode="url", x_xpath1=pic_xpath + f'[{j}]/@src')
print(str(j), p_title, p_href)
self.down_picture(pic_path=f"./selenium./02118_信息检索/picture/{str(i)}_{p_title}", pic_name=str(j) + "_" + p_title, pic_url=p_href)
self.wait_time(1, p_title)
def main_selenium(self):
# res_html = self.get_selenium_html()
self.get_catalog(file_name=r'D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html')
if __name__ == "__main__":
Test().main_selenium()
D:\TestFiles\Python3\python.exe D:/Mytest/Svnbucket/python/demo/crawler/20210312_自考100_试卷下载/20210312_自考100_试卷下载_英语二.py
----四川自考02118《信息检索》全真模拟试题(二) 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html:12条数据
------------------------
四川自考02118《信息检索》全真模拟试题(二)
2020-06-28
https://sc.exam100.net/html/2020/lgmn_0628/12874.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:0条数据
------------------------
四川自考02118《信息检索》全真模拟试题(一)
2019-04-23
https://sc.exam100.net/html/2019/lgmn_0423/11912.html
0%| | 0/1 [00:00<?, ?it/s]
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:0条数据
----四川自考02118《信息检索》全真模拟试题(一) 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2013年1月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11105.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:8条数据
1 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085902846.jpg
2 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085902656.jpg
3 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085902862.jpg
4 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085902989.jpg
5 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085903598.jpg
6 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085903881.jpg
7 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085903151.jpg
8 四川2013年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085903196.jpg
----四川2013年1月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2013年4月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11106.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:8条数据
1 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085927732.jpg
2 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085927520.jpg
3 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085928677.jpg
4 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085928634.jpg
5 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085928255.jpg
6 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085928362.jpg
7 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085928521.jpg
8 四川2013年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085929116.jpg
----四川2013年4月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2014年1月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11107.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:8条数据
1 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085954754.jpg
2 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085955636.jpg
3 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085955450.jpg
4 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085955641.jpg
5 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085955260.jpg
6 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085956657.jpg
7 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085956602.jpg
8 四川2014年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311085956423.jpg
----四川2014年1月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2014年4月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11108.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:7条数据
1 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090020505.jpg
2 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090020612.jpg
3 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090021404.jpg
4 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090021760.jpg
5 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090021539.jpg
6 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090021199.jpg
7 四川2014年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090022614.jpg
----四川2014年4月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2015年1月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11109.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:8条数据
1 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090048376.jpg
2 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090049195.jpg
3 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090049907.jpg
4 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090049134.jpg
5 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090049995.jpg
6 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090049182.jpg
7 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090050162.jpg
8 四川2015年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090050857.jpg
----四川2015年1月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2016年1月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11110.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:7条数据
1 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090114251.jpg
2 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090114194.jpg
3 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090115946.jpg
4 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090115689.jpg
5 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090115480.jpg
6 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090115692.jpg
7 四川2016年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090115164.jpg
----四川2016年1月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2016年4月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11111.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:7条数据
1 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090142880.jpg
2 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090142878.jpg
3 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090142599.jpg
4 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090142693.jpg
5 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090142246.jpg
6 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090143115.jpg
7 四川2016年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090143970.jpg
----四川2016年4月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2017年1月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11112.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:4条数据
1 四川2017年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090205269.jpg
2 四川2017年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090205472.jpg
3 四川2017年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090205620.jpg
4 四川2017年1月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090206375.jpg
----四川2017年1月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2017年4月自考02118《信息检索》试题
2018-03-01
https://sc.exam100.net/html/2018/lgl_0301/11113.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:4条数据
1 四川2017年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090226466.jpg
2 四川2017年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090228895.jpg
3 四川2017年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090228275.jpg
4 四川2017年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2019/0311/20190311090228220.jpg
----四川2017年4月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
------------------------
四川2018年4月自考02118《信息检索》试题
2018-12-17
https://sc.exam100.net/html/2018/lgl_1217/7844.html
D:\Mytest\Svnbucket\python\demo\crawler\20210312_自考100_试卷下载\selenium\02118_信息检索\html\02118_信息检索.html_:3条数据
1 四川2018年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2018/1217/20181217120427372.jpg
2 四川2018年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2018/1217/20181217120427407.jpg
3 四川2018年4月自考02118《信息检索》试题 https://www.exam100.net/uploadfile/2018/1217/20181217120427926.jpg
----四川2018年4月自考02118《信息检索》试题 1 : 100%|██████████| 1/1 [00:01<00:00, 1.00s/it]
Process finished with exit code 0