forked from openai/chatgpt-retrieval-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnapavalley_scraper.py
57 lines (43 loc) · 1.63 KB
/
napavalley_scraper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import requests
from bs4 import BeautifulSoup
import time
import json
def get_blog_urls(soup):
urls = []
links = soup.find_all('a', {'class': 'btn btn-normal'}, href=True)
for link in links:
urls.append(link['href'])
return urls
def get_blog_data(url):
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html, 'html.parser')
title_div = soup.find('div', {'class': 'intro-head-title'})
title = title_div.h1.get_text(strip=True)
body_div = soup.find('div', {'class': 'section section-white blog-detail'})
list_share_div = body_div.find('div', {'class': 'list-share'})
if list_share_div:
list_share_div.extract()
body = body_div.get_text(strip=True, separator=' ')
return {'url': url, 'title': title, 'text': body}
base_url = 'https://www.napavalley.com/blog'
all_blog_data = []
for current_page in range(1, 24): # Iterate from page 1 to 23, inclusive
page_url = f'{base_url}/page/{current_page}/'
print(f'Scraping page {page_url}')
response = requests.get(page_url)
html = response.content
if response.status_code != 200:
break
soup = BeautifulSoup(html, 'html.parser')
blog_urls = get_blog_urls(soup)
for blog_url in blog_urls:
print(f'Fetching data from {blog_url}')
blog_data = get_blog_data(blog_url)
all_blog_data.append(blog_data)
time.sleep(1) # 1-second delay
time.sleep(1) # 1-second delay between pages
print('Scraping complete.')
# Save results to a JSON file
with open('napavalley_blog_data.json', 'w') as json_file:
json.dump(all_blog_data, json_file, indent=4)