from selenium import webdriverfrom selenium.webdriver.firefox.options import Optionsfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECimport selenium.webdriver.support.ui as uifrom bs4 import BeautifulSoupimport timeimport jsonimport randomimport requestsdef get_cookies(): options = Options() # options.add_argument('-headless') driver = webdriver.Firefox(executable_path='/Users/viemaxwei/Downloads/geckodriver 2', options=options) url = "http://www.pss-system.gov.cn/sipopublicsearch/portal/uiIndex.shtml" driver.get(url) time.sleep(1) driver.find_element_by_id("j_username").send_keys("weimengxin2018") driver.find_element_by_id("j_password_show").send_keys("adwahads6136879") # html_login = driver.page_source valid = input("请手动输入验证码:") driver.find_element_by_id("j_validation_code").send_keys(valid) driver.find_element_by_link_text("登录").click() time.sleep(3) cookie = driver.get_cookies() cookie_1 = cookie[0]["name"] + "=" + cookie[0]["value"] cookie_2 = cookie[1]["name"] + "=" + cookie[1]["value"] cookie_3 = cookie[2]["name"] + "=" + cookie[2]["value"] cookie_all = cookie_3 + "; " + cookie_1 + "; " + cookie_2 cookies_head = dict() cookies_head["cookies_are"] = cookie_all driver.quit() return cookies_headdef requests_login_pss_system(date, pages): res = [] result = [] # pages = 1 url = "http://www.pss-system.gov.cn/sipopublicsearch/patentsearch/showSearchResult-startWa.shtml" head = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36', 'Connection': 'keep-alive', 'Origin': 'http://www.pss-system.gov.cn'} # while True: info = {"searchCondition.searchExp": "(申请日='%s') AND 公开国家/地区/组织=(HK OR MO OR TW OR (发明类型=('I' OR 'U' OR 'D') AND 公开国家/地区/组织=(CN)))" % date, "searchCondition.executableSearchExp": "VDB:((APD='%s' AND (CC='HK' OR CC='MO' OR CC='TW' OR ((DOC_TYPE='I' OR DOC_TYPE='U' OR DOC_TYPE='D') AND CC='CN'))))" % date, "resultPagination.start": (pages - 1) * 12, "resultPagination.limit": 12, "searchCondition.literatureSF": "(申请日='%s') AND 公开国家/地区/组织=(HK OR MO OR TW OR (发明类型=('I' OR 'U' OR 'D') AND 公开国家/地区/组织=(CN)))" % date} s = requests.session() # s.keep_alive = False html = s.post(url, headers=head, data=info, cookies=cookies_headless).json() time.sleep(2 + random.random() * 2.5) page = html["searchResultDTO"]["pagination"]["totalCount"] if page == -1: print("page=-1,第%d页" % pages) pages += 1 s.close() else: print("共%d条记录" % page) if html["searchResultDTO"]["searchResultRecord"] != []: for i in html["searchResultDTO"]["searchResultRecord"]: lawStatus = i["lawStatus"] patentType = i["patentType"] name = i["fieldMap"]["TIVIEW"] fnum = i["fieldMap"]["FNUM"] cpnum = i["fieldMap"]["CPNUM"] pnum = i["fieldMap"]["PNUM"] VID = i["fieldMap"]["VID"] APSN = i["fieldMap"]["APSN"] APD = i["fieldMap"]["APD"] PN_BAK = i["fieldMap"]["PN_BAK"] PD = i["fieldMap"]["PD"] # 公开日 IC = i["fieldMap"]["IC"] # IPC分类号 # CPC = i["fieldMap"]["CPC"] # CPC分类号 PAVIEW = i["fieldMap"]["PAVIEW"] # 申请人 INVIEW = i["fieldMap"]["INVIEW"] # 发明人 AA = i["fieldMap"]["AA"] # 地址 AGT = i["fieldMap"]["AGT"] # 代理人 AGY = i["fieldMap"]["AGY"] # 代理机构 OWNER_STATUS = i["fieldMap"]["AGY"] # 代理机构 res.extend([lawStatus, patentType, name, fnum, cpnum, pnum, VID, APSN, APD, PN_BAK, PD, IC, PAVIEW, INVIEW, AA, AGT, AGY, OWNER_STATUS]) print(res) middle = res.copy() result.append(middle) res.clear() pages += 1 time.sleep(0.7) s.close() return result else: print("检索完毕!")def run_fiction(date, start_page, end_page, list_con): start = time.time() try: for ind in range(start_page, end_page): # 1001 1101 print("正在检索 %d 页" % ind) test = requests_login_pss_system(date, ind) list_con.extend(test) if ind % 4 == 0: print("Code冷却中。。。") time.sleep(3 + random.random() * 7) except: print("完成!") end = time.time() print("20170101用时:%f" % (end - start))res_10 = []cookies_headless = get_cookies()run = run_fiction("20170110", 240, 1501, res_10)