|
10 | 10 | import util
|
11 | 11 | import driver_control
|
12 | 12 |
|
13 |
| -parser = argparse.ArgumentParser(description="這是一個簡單的範例 argparse 程式") |
14 |
| -parser.add_argument("--shop_name", help="提供一個名字作為參數", required=True) |
| 13 | +parser = argparse.ArgumentParser(description="自動飯用爬蟲程式") |
| 14 | +parser.add_argument("--shop_name", type=str, help="網站名稱", required=True) |
| 15 | +parser.add_argument("--test", action='store_true', help="測試選項,只進行一次運作") |
15 | 16 | args = parser.parse_args()
|
16 | 17 |
|
17 | 18 | # 設定logging的配置
|
|
20 | 21 | myhome = Path.cwd()
|
21 | 22 |
|
22 | 23 | logging.basicConfig(
|
23 |
| - filename=myhome / 'logfile' / '{shop_name}.log', |
| 24 | + filename=myhome / 'logfile' / f'{args.shop_name}.log', |
24 | 25 | level=logging.INFO,
|
25 | 26 | format='%(asctime)s - %(levelname)s - %(message)s'
|
26 | 27 | )
|
27 | 28 |
|
28 |
| -# 使用logging對象輸出日誌 |
29 |
| -logging.info('This is a test message') |
30 | 29 |
|
31 |
| -a, b = util.read_csv(Path.cwd() / 'eddiebauer_test.csv') |
32 | 30 | listsite, site_feature = util.read_csv(
|
33 |
| - (myhome / 'cite_file' / f'{args.shop_name}.csv').resolve()) |
| 31 | + (myhome / 'cite_file' / f'{args.shop_name}.csv')) |
34 | 32 | with open('driver_path', 'r', encoding='big5') as f:
|
35 | 33 | tmp = f.read()
|
36 | 34 | driver = util.new_driver(myhome / tmp)
|
37 | 35 | with open('cite_fathers.json', 'r') as file:
|
38 | 36 | father: str = json.load(file)[args.shop_name]
|
| 37 | +if args.test: |
| 38 | + driver.get(url=listsite[0]) |
| 39 | + imgpath = util.check_imgpath(imgpath=myhome / args.shop_name, |
| 40 | + imgfile=site_feature[0]) |
| 41 | + time.sleep(5) |
| 42 | + driver_control.scroll_to_bottom_and_wait(driver=driver) |
| 43 | + try: |
| 44 | + parent_element = driver.find_element("class name", father) |
| 45 | + except NoSuchElementException: |
| 46 | + logging.error('element not find in parent_element') |
| 47 | + try: |
| 48 | + child_elements = parent_element.find_elements('xpath', "./*") |
| 49 | + except NoSuchElementException: |
| 50 | + logging.error('element not find in child_elements') |
| 51 | + for element in child_elements: |
| 52 | + tmp = util.capture(ele=element, path=imgpath) |
| 53 | + print(tmp) |
| 54 | + import sys |
| 55 | + sys.exit() |
39 | 56 | for i in len(listsite):
|
40 | 57 | driver.get(url=listsite[i])
|
41 | 58 | time.sleep(5)
|
|
0 commit comments