from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import requests
from bs4 import BeautifulSoup
import csv
import ssl
import re
import time
from prettyprinter import cpprint
ssl._create_default_https_context = ssl._create_unverified_context
def get_newURL(surname):
if __name__ == "__main__":
options = Options()
options.add_argument('-headless')
global browser
browser = webdriver.Firefox(executable_path=r"/Users/viemaxwei/Downloads/geckodriver", firefox_options=options)
# browser = webdriver.Firefox(executable_path=r"/Users/viemaxwei/Downloads/geckodriver")
browser.get('http://search.library.sh.cn/jiapu/bSearch.htm')
input_str = browser.find_element_by_name('expr')
input_str.send_keys(surname)
browser.find_element_by_xpath("//*[@value='检索']").click()
time.sleep(1.5)
browser.switch_to.window(browser.window_handles[1])
global newurl
newurl = browser.current_url
browser.quit()
def get_next_page(i, new_url):
global url_new
global browser_1
try:
if __name__ == "__main__":
options = Options()
options.add_argument('-headless')
browser_1 = webdriver.Firefox(executable_path=r"/Users/viemaxwei/Downloads/geckodriver", firefox_options=options)
# browser_1 = webdriver.Firefox(executable_path=r"/Users/viemaxwei/Downloads/geckodriver")
browser_1.get(new_url)
browser_1.find_element_by_xpath("//*[@value='下页']").click()
browser_1.switch_to.window(browser_1.window_handles[0])
url_new = browser_1.current_url
i += 1
print("检索第%d页的url" % i)
single_url_collector(url_new)
browser_1.quit()
return get_next_page(i, url_new)
except:
browser_1.quit()
print("<---检索完成--->")
with open("/Users/viemaxwei/Downloads/surname_1.csv", "rt") as sur:
cin = csv.reader(sur)
surname = [i for i in cin]
surname_dict = dict(surname)
single_url_set = []
def single_url_collector(url):
single_url_set.append(url)
return single_url_set
total_url = {}
def get_single_url_set():
for index in surname_dict:
print("<---现在自动检索_" + surname_dict[index] + "氏_数据--->")
print("检索第1页的url")
get_newURL(surname_dict[index] + "氏")
single_url_set.clear()
single_url_set.append(newurl)
get_next_page(1, newurl)
url_set_copy = single_url_set.copy()
total_url[surname_dict[index]] = url_set_copy
# cpprint(total_url)
start = time.time()
get_single_url_set()
end = time.time()
time_total = (end - start)/60
print("====================")
print("全部完成!!! 共用时__%f__分钟" % (time_total))
2018-01-18
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 猫犬系这个形容,最早来自日本女孩对目标男友的划分,但在最近的警匪电影和职场,也常常见之以这种组合。 改编自中国心理...