kenken999 commited on
Commit
25e1c5e
1 Parent(s): 8545387
Files changed (2) hide show
  1. README.md +1 -0
  2. polls/crawl.py +71 -0
README.md CHANGED
@@ -16,3 +16,4 @@ export OPENAI_API_KEY="*************"
16
  export MODEL_NAME="llama3-8b-8192"
17
  export LOCAL_MODEL=true
18
  for interpreter
 
 
16
  export MODEL_NAME="llama3-8b-8192"
17
  export LOCAL_MODEL=true
18
  for interpreter
19
+ https://zenn.dev/singularity/articles/231adc99e78a3d
polls/crawl.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+ import json
4
+ from selenium import webdriver
5
+ from selenium.webdriver.chrome.options import Options
6
+ import time
7
+ import re
8
+ import os
9
+
10
+ # URL of the your website
11
+ url = 'https://XXX.com'
12
+
13
+ # Set Chrome options to enable headless mode
14
+ chrome_options = Options()
15
+ chrome_options.add_argument('--headless')
16
+ chrome_options.add_argument('--no-sandbox')
17
+ chrome_options.add_argument('--disable-dev-shm-usage')
18
+
19
+ # Set the path to chromedriver executable
20
+ chromedriver_path = '/usr/local/bin/chromedriver'
21
+
22
+ # Create a new Chrome instance
23
+ driver = webdriver.Chrome(options=chrome_options)
24
+
25
+ # Load the website
26
+ driver.get(url)
27
+
28
+ # Wait for the page to fully render
29
+ time.sleep(5)
30
+
31
+ # Extract the rendered HTML
32
+ html = driver.page_source
33
+
34
+ # Close the Chrome instance
35
+ driver.quit()
36
+
37
+ # Parse the HTML using BeautifulSoup
38
+ soup = BeautifulSoup(html, 'html.parser')
39
+
40
+ data = {}
41
+
42
+ # h1〜h4のタグを取得
43
+ headings = soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5'])
44
+ (h1_text,h2_text,h3_text,h4_text,h5_text)=("","","","","")
45
+ for heading in headings:
46
+ if heading.name == 'h1':
47
+ h1_text = heading.text
48
+ key = h1_text
49
+ elif heading.name == 'h2':
50
+ h2_text = heading.text
51
+ key = f"{h1_text}-{h2_text}"
52
+ elif heading.name == 'h3':
53
+ h3_text = heading.text
54
+ key = f"{h1_text}-{h2_text}-{h3_text}"
55
+ elif heading.name == 'h4':
56
+ h4_text = heading.text
57
+ key = f"{h1_text}-{h2_text}-{h3_text}-{h4_text}"
58
+ elif heading.name == 'h5':
59
+ h5_text = heading.text
60
+ key = f"{h1_text}-{h2_text}-{h3_text}-{h5_text}"
61
+
62
+ # 次の要素のテキストを取得
63
+ sibling = heading.find_next_sibling()
64
+ value = ''
65
+ while sibling and not sibling.name in ['h1', 'h2', 'h3', 'h4', 'h5']:
66
+ value += sibling.text
67
+ sibling = sibling.find_next_sibling()
68
+
69
+ data[key] = value.strip()
70
+
71
+ print(len(data),(data.keys()))