-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt.txt
826 lines (599 loc) · 27.7 KB
/
chatgpt.txt
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
You said:
write some python code that will read a website and convert a table from that web site into a CSV file
ChatGPT said:
ChatGPT
To scrape a table from a website and convert it to a CSV file in Python, you can use libraries like requests to fetch the webpage, and BeautifulSoup from bs4 to parse the HTML and extract the table data. The csv module is used to write the extracted data to a CSV file.
Here’s an example Python script that demonstrates this process:
Requirements:
requests: To fetch the webpage content.
beautifulsoup4: To parse the HTML and extract the table.
pandas (optional but useful for easier data handling).
You can install these libraries with pip if you don't have them already:
pip install requests beautifulsoup4 pandas
Python Script:
import requests
from bs4 import BeautifulSoup
import csv
def scrape_table_to_csv(url, csv_filename):
# Send an HTTP request to the website
response = requests.get(url)
if response.status_code != 200:
print("Failed to retrieve the webpage")
return
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the first table in the webpage
table = soup.find('table')
if not table:
print("No table found on the webpage")
return
# Extract table headers (if any)
headers = []
header_row = table.find('tr')
if header_row:
header_cells = header_row.find_all(['th', 'td'])
headers = [cell.get_text(strip=True) for cell in header_cells]
# Extract table rows
rows = table.find_all('tr')[1:] # Skip the header row
table_data = []
for row in rows:
cells = row.find_all('td')
row_data = [cell.get_text(strip=True) for cell in cells]
table_data.append(row_data)
# Write the data to a CSV file
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
# Write headers first (if any)
if headers:
writer.writerow(headers)
# Write table rows
writer.writerows(table_data)
print(f"Data has been written to {csv_filename}")
# Example usage
url = 'https://example.com/your-webpage-with-table'
csv_filename = 'output.csv'
scrape_table_to_csv(url, csv_filename)
How the code works:
requests.get(url): Sends an HTTP GET request to the website URL.
BeautifulSoup(response.content, 'html.parser'): Parses the HTML content using BeautifulSoup.
soup.find('table'): Finds the first <table> element in the HTML.
Extract headers: If the table has headers (inside <th> elements), they are extracted.
Extract rows: Iterates over each <tr> row in the table and extracts the cell data (from <td> elements).
Write to CSV: The headers (if found) and the rows are written to a CSV file using Python's csv module.
Notes:
You may need to adjust the table parsing if the structure of the table is more complex (e.g., if there are nested tables, spans, etc.).
If the website is dynamically loaded (using JavaScript), you might need to use something like Selenium or Playwright to render the page before scraping.
Example: How to adjust for dynamic content
If the page requires JavaScript to load the table, requests won't be enough. You can use Selenium to render the page:
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import csv
import time
def scrape_dynamic_table_to_csv(url, csv_filename):
# Initialize the WebDriver (make sure you have the correct driver installed)
driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # You can also use Firefox, etc.
driver.get(url)
# Wait for the page to load (you may need to adjust this sleep time)
time.sleep(3)
# Parse the page source
soup = BeautifulSoup(driver.page_source, 'html.parser')
# Close the WebDriver
driver.quit()
# Follow the same logic as above to scrape and save the table to CSV
table = soup.find('table')
if not table:
print("No table found on the webpage")
return
headers = []
header_row = table.find('tr')
if header_row:
header_cells = header_row.find_all(['th', 'td'])
headers = [cell.get_text(strip=True) for cell in header_cells]
rows = table.find_all('tr')[1:]
table_data = []
for row in rows:
cells = row.find_all('td')
row_data = [cell.get_text(strip=True) for cell in cells]
table_data.append(row_data)
# Write the data to CSV
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
if headers:
writer.writerow(headers)
writer.writerows(table_data)
print(f"Data has been written to {csv_filename}")
# Example usage
url = 'https://example.com/dynamic-table-page'
csv_filename = 'dynamic_output.csv'
scrape_dynamic_table_to_csv(url, csv_filename)
This version uses Selenium to load the page and then extracts the table once the page content is fully loaded.
Let me know if you'd like further clarifications or adjustments!
You said:
adjust this to have it read in a list of URLs from stdin and fetch each one. Name the output based on the filename of the html file in the url
ChatGPT said:
ChatGPT
To modify the code so that it can read a list of URLs from standard input (stdin), fetch the content from each URL, and name the output CSV files based on the HTML filename in the URL, you can adjust the script as follows:
Use sys.stdin.read() to read multiple URLs.
Parse the filename from the URL to create a CSV filename.
Loop through the list of URLs, fetch the table, and save each result to a CSV file.
Modified Python Script:
import requests
from bs4 import BeautifulSoup
import csv
import sys
import os
from urllib.parse import urlparse
def scrape_table_to_csv(url):
# Send an HTTP request to the website
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to retrieve the webpage: {url}")
return
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the first table in the webpage
table = soup.find('table')
if not table:
print(f"No table found on the webpage: {url}")
return
# Extract table headers (if any)
headers = []
header_row = table.find('tr')
if header_row:
header_cells = header_row.find_all(['th', 'td'])
headers = [cell.get_text(strip=True) for cell in header_cells]
# Extract table rows
rows = table.find_all('tr')[1:] # Skip the header row
table_data = []
for row in rows:
cells = row.find_all('td')
row_data = [cell.get_text(strip=True) for cell in cells]
table_data.append(row_data)
# Generate a filename based on the URL's HTML file name
parsed_url = urlparse(url)
filename = os.path.basename(parsed_url.path)
# If there's no file in the URL (e.g., just a directory), use 'output.csv'
if not filename or filename == '/':
filename = 'output.html'
# Remove any query parameters or fragments from the filename
filename = filename.split('?')[0].split('#')[0]
# Change the file extension to CSV
csv_filename = f"{filename}.csv"
# Write the data to the CSV file
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
# Write headers first (if any)
if headers:
writer.writerow(headers)
# Write table rows
writer.writerows(table_data)
print(f"Data has been written to {csv_filename} from {url}")
# Read URLs from standard input (stdin)
def read_urls_from_stdin():
urls = sys.stdin.read().splitlines()
return urls
# Main function to process each URL
def main():
urls = read_urls_from_stdin()
if not urls:
print("No URLs provided.")
return
for url in urls:
scrape_table_to_csv(url)
# Run the script
if __name__ == "__main__":
main()
Explanation of Changes:
Reading URLs from stdin:
The read_urls_from_stdin() function reads all URLs from standard input (sys.stdin.read()), splitting them by newline (splitlines()).
This allows you to pass multiple URLs to the script at once (e.g., via a file or pipe).
Generating Output Filename:
urlparse(url) is used to extract parts of the URL. The os.path.basename(parsed_url.path) method retrieves the last part of the path (i.e., the filename).
The script handles cases where the URL might not have a filename in its path (e.g., https://example.com/), defaulting to output.html.
The filename extension is changed to .csv (e.g., example.html becomes example.csv).
Looping Through URLs:
The script loops over the list of URLs and processes each URL by calling scrape_table_to_csv(url).
How to Run:
With Input from a File: You can provide URLs through a file (e.g., urls.txt), and then run the script with:
python scrape_to_csv.py < urls.txt
With URLs Directly from stdin: Alternatively, you can pipe URLs directly to the script:
echo -e "https://example.com/page1\nhttps://example.com/page2" | python scrape_to_csv.py
Example:
Given the following URL:
https://example.com/table-page1
The output will be saved as table-page1.csv.
Notes:
Error Handling: If the table is not found on a given page, the script will skip it and print a message.
File Naming: If the URL has query parameters (e.g., example.com/page?param=1), those are ignored when determining the filename.
This should allow you to handle a list of URLs efficiently and save each table as a corresponding CSV file.
You said:
also make this code remove from the data any text that is a number enclosed in parentheses
ChatGPT said:
ChatGPT
To modify the code so that it removes any text that is a number enclosed in parentheses from the table data, you can use regular expressions to identify and remove such text. We will use Python's re module for this task.
Steps to Modify:
Use Regular Expressions: We'll define a regex pattern that matches numbers enclosed in parentheses (e.g., (123) or (4.56)).
Apply this Regex: We'll apply this regex to each cell of the table before writing it to the CSV file.
Here’s how the code can be adjusted to meet your requirement:
Modified Python Script:
import requests
from bs4 import BeautifulSoup
import csv
import sys
import os
from urllib.parse import urlparse
import re
# Function to remove numbers enclosed in parentheses
def remove_numbers_in_parentheses(text):
# Regular expression to match numbers in parentheses (including decimal numbers)
return re.sub(r'\(\s*\d+(\.\d+)?\s*\)', '', text)
def scrape_table_to_csv(url):
# Send an HTTP request to the website
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to retrieve the webpage: {url}")
return
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the first table in the webpage
table = soup.find('table')
if not table:
print(f"No table found on the webpage: {url}")
return
# Extract table headers (if any)
headers = []
header_row = table.find('tr')
if header_row:
header_cells = header_row.find_all(['th', 'td'])
headers = [remove_numbers_in_parentheses(cell.get_text(strip=True)) for cell in header_cells]
# Extract table rows
rows = table.find_all('tr')[1:] # Skip the header row
table_data = []
for row in rows:
cells = row.find_all('td')
row_data = [remove_numbers_in_parentheses(cell.get_text(strip=True)) for cell in cells]
table_data.append(row_data)
# Generate a filename based on the URL's HTML file name
parsed_url = urlparse(url)
filename = os.path.basename(parsed_url.path)
# If there's no file in the URL (e.g., just a directory), use 'output.csv'
if not filename or filename == '/':
filename = 'output.html'
# Remove any query parameters or fragments from the filename
filename = filename.split('?')[0].split('#')[0]
# Change the file extension to CSV
csv_filename = f"{filename}.csv"
# Write the data to the CSV file
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
# Write headers first (if any)
if headers:
writer.writerow(headers)
# Write table rows
writer.writerows(table_data)
print(f"Data has been written to {csv_filename} from {url}")
# Read URLs from standard input (stdin)
def read_urls_from_stdin():
urls = sys.stdin.read().splitlines()
return urls
# Main function to process each URL
def main():
urls = read_urls_from_stdin()
if not urls:
print("No URLs provided.")
return
for url in urls:
scrape_table_to_csv(url)
# Run the script
if __name__ == "__main__":
main()
Explanation of Changes:
remove_numbers_in_parentheses function:
This function uses the regex r'\(\s*\d+(\.\d+)?\s*\)' to match numbers enclosed in parentheses.
\( and \) match the literal parentheses.
\d+ matches one or more digits.
(\.\d+)? optionally matches a decimal point followed by one or more digits.
\s* allows for optional whitespace inside the parentheses.
It then uses re.sub() to remove any such matches from the text.
Applying the function:
The remove_numbers_in_parentheses function is applied to each cell's text in both the table headers and the rows, removing any numbers enclosed in parentheses.
Example:
Given the following table:
Name Value
Item 1 $200 (5)
Item 2 $150 (10.5)
Item 3 $300
The output CSV will look like this:
Name Value
Item 1 $200
Item 2 $150
Item 3 $300
The numbers in parentheses are removed from the "Value" column.
How to Run:
With Input from a File: You can provide URLs through a file (e.g., urls.txt), and then run the script with:
python scrape_to_csv.py < urls.txt
With URLs Directly from stdin: Alternatively, you can pipe URLs directly to the script:
echo -e "https://example.com/page1\nhttps://example.com/page2" | python scrape_to_csv.py
Notes:
The function remove_numbers_in_parentheses ensures that no numbers in parentheses remain in the table data before saving to CSV.
If there are no tables or the table structure is complex, the script will print a message and skip the page.
This approach will now properly clean the table data by removing numbers in parentheses, as required.
You said:
Also have it search the text of the web page for a date. Take that date and append it as the last field in every line of the csv file
ChatGPT said:
ChatGPT
To enhance the script so that it searches the webpage for a date and appends that date as the last field in every row of the CSV file, you can use regular expressions to find the date in the page content. Once the date is found, it can be added to each row of the table before writing it to the CSV.
Steps to Modify:
Search for a Date: We'll use a regex pattern to find a date in the text. The date format can vary (e.g., MM/DD/YYYY, YYYY-MM-DD, DD MMM YYYY, etc.), so we'll create a general pattern that captures common date formats.
Add the Date to Each Row: Once a date is found, append it as the last field in each row before writing to the CSV.
Example Date Regex:
The regex r'\b(?:\d{1,2}[/-]\d{1,2}[/-]\d{2,4}|\d{4}[/-]\d{1,2}[/-]\d{1,2})\b' captures various date formats such as MM/DD/YYYY, YYYY-MM-DD, DD/MM/YYYY, and others.
Here’s the modified script:
Modified Python Script:
import requests
from bs4 import BeautifulSoup
import csv
import sys
import os
from urllib.parse import urlparse
import re
# Function to remove numbers enclosed in parentheses
def remove_numbers_in_parentheses(text):
# Regular expression to match numbers in parentheses (including decimal numbers)
return re.sub(r'\(\s*\d+(\.\d+)?\s*\)', '', text)
# Function to extract the first date found on the webpage
def extract_date_from_page(content):
# Regular expression pattern for various date formats (e.g., MM/DD/YYYY, YYYY-MM-DD, etc.)
date_pattern = r'\b(?:\d{1,2}[/-]\d{1,2}[/-]\d{2,4}|\d{4}[/-]\d{1,2}[/-]\d{1,2}|\d{1,2}\s+[A-Za-z]+\s+\d{4})\b'
match = re.search(date_pattern, content)
if match:
return match.group(0)
return None
def scrape_table_to_csv(url):
# Send an HTTP request to the website
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to retrieve the webpage: {url}")
return
# Extract the page content (HTML) to search for a date
page_content = response.text
# Extract date from the page content
page_date = extract_date_from_page(page_content)
if page_date:
print(f"Found date: {page_date}")
else:
print(f"No date found on the page: {url}")
page_date = "Unknown Date"
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the first table in the webpage
table = soup.find('table')
if not table:
print(f"No table found on the webpage: {url}")
return
# Extract table headers (if any)
headers = []
header_row = table.find('tr')
if header_row:
header_cells = header_row.find_all(['th', 'td'])
headers = [remove_numbers_in_parentheses(cell.get_text(strip=True)) for cell in header_cells]
# Extract table rows
rows = table.find_all('tr')[1:] # Skip the header row
table_data = []
for row in rows:
cells = row.find_all('td')
row_data = [remove_numbers_in_parentheses(cell.get_text(strip=True)) for cell in cells]
# Append the extracted date to the end of the row data
row_data.append(page_date)
table_data.append(row_data)
# Generate a filename based on the URL's HTML file name
parsed_url = urlparse(url)
filename = os.path.basename(parsed_url.path)
# If there's no file in the URL (e.g., just a directory), use 'output.csv'
if not filename or filename == '/':
filename = 'output.html'
# Remove any query parameters or fragments from the filename
filename = filename.split('?')[0].split('#')[0]
# Change the file extension to CSV
csv_filename = f"{filename}.csv"
# Write the data to the CSV file
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
# Write headers first (if any)
if headers:
headers.append("Date") # Add 'Date' as the last header
writer.writerow(headers)
# Write table rows
writer.writerows(table_data)
print(f"Data has been written to {csv_filename} from {url}")
# Read URLs from standard input (stdin)
def read_urls_from_stdin():
urls = sys.stdin.read().splitlines()
return urls
# Main function to process each URL
def main():
urls = read_urls_from_stdin()
if not urls:
print("No URLs provided.")
return
for url in urls:
scrape_table_to_csv(url)
# Run the script
if __name__ == "__main__":
main()
Explanation of Changes:
extract_date_from_page(content):
This function uses a regular expression to search for a date in the page's text.
The regex pattern handles several common date formats, including:
MM/DD/YYYY
YYYY-MM-DD
DD MMM YYYY (e.g., 12 Mar 2024)
If a date is found, it returns the date as a string; otherwise, it returns "Unknown Date".
Appending the Date to Each Row:
After extracting the date from the page, the script appends the date as the last field in each row of the table.
The headers are also updated to include a column for the date, labeled "Date".
Filename Generation:
The filename is still based on the last segment of the URL path, and the file extension is changed to .csv.
If no table is found or there is an error, the script prints a message and skips to the next URL.
How the Date Extraction Works:
When the page content is fetched, the function extract_date_from_page searches for a date in the text. If a valid date is found, it is appended to every row in the table as the last field.
If no date is found, the script appends "Unknown Date" to the rows.
Example:
Given the following table on the webpage:
Name Value
Item 1 $200 (5)
Item 2 $150 (10.5)
Item 3 $300
And a date found on the page like 2024-11-05, the output CSV will look like this:
Name Value Date
Item 1 $200 2024-11-05
Item 2 $150 2024-11-05
Item 3 $300 2024-11-05
How to Run:
With Input from a File: You can provide URLs through a file (e.g., urls.txt), and then run the script with:
python scrape_to_csv.py < urls.txt
With URLs Directly from stdin: Alternatively, you can pipe URLs directly to the script:
echo -e "https://example.com/page1\nhttps://example.com/page2" | python scrape_to_csv.py
Notes:
Date Format: The regex can be adjusted for more specific date formats depending on your needs.
Multiple Dates: This script captures only the first date it finds. If you need to handle multiple dates or extract them in a more complex way, you can modify the logic accordingly.
This modification ensures that every row in your CSV file will have the corresponding date from the page appended as the last field.
You said:
repeat the last instruction, but when searching for a date, look for a date in the format of a 3 letter month abbreviation, followed by the date number, then a comma, then a 4 digit year. An example would be Apr 23, 2019.
ChatGPT said:
ChatGPT
To adjust the code to specifically search for dates in the format of a three-letter month abbreviation, followed by the day number, a comma, and then a four-digit year (e.g., Apr 23, 2019), we will need to modify the regular expression.
Updated Regular Expression:
The new regex will look for dates in the format of:
A three-letter month abbreviation (e.g., Jan, Feb, Mar, Apr, etc.).
A day number (1 or 2 digits).
A comma, followed by a four-digit year.
Here’s how we can modify the script:
Modified Python Script:
import requests
from bs4 import BeautifulSoup
import csv
import sys
import os
from urllib.parse import urlparse
import re
# Function to remove numbers enclosed in parentheses
def remove_numbers_in_parentheses(text):
# Regular expression to match numbers in parentheses (including decimal numbers)
return re.sub(r'\(\s*\d+(\.\d+)?\s*\)', '', text)
# Function to extract the first date found on the webpage (in format: "Mon dd, yyyy")
def extract_date_from_page(content):
# Regular expression pattern for a date like "Apr 23, 2019"
date_pattern = r'\b([A-Za-z]{3})\s*(\d{1,2}),\s*(\d{4})\b'
match = re.search(date_pattern, content)
if match:
# Return the date in the format "Mon dd, yyyy"
return match.group(0)
return None
def scrape_table_to_csv(url):
# Send an HTTP request to the website
response = requests.get(url)
if response.status_code != 200:
print(f"Failed to retrieve the webpage: {url}")
return
# Extract the page content (HTML) to search for a date
page_content = response.text
# Extract date from the page content
page_date = extract_date_from_page(page_content)
if page_date:
print(f"Found date: {page_date}")
else:
print(f"No date found on the page: {url}")
page_date = "Unknown Date"
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, 'html.parser')
# Find the first table in the webpage
table = soup.find('table')
if not table:
print(f"No table found on the webpage: {url}")
return
# Extract table headers (if any)
headers = []
header_row = table.find('tr')
if header_row:
header_cells = header_row.find_all(['th', 'td'])
headers = [remove_numbers_in_parentheses(cell.get_text(strip=True)) for cell in header_cells]
# Extract table rows
rows = table.find_all('tr')[1:] # Skip the header row
table_data = []
for row in rows:
cells = row.find_all('td')
row_data = [remove_numbers_in_parentheses(cell.get_text(strip=True)) for cell in cells]
# Append the extracted date to the end of the row data
row_data.append(page_date)
table_data.append(row_data)
# Generate a filename based on the URL's HTML file name
parsed_url = urlparse(url)
filename = os.path.basename(parsed_url.path)
# If there's no file in the URL (e.g., just a directory), use 'output.csv'
if not filename or filename == '/':
filename = 'output.html'
# Remove any query parameters or fragments from the filename
filename = filename.split('?')[0].split('#')[0]
# Change the file extension to CSV
csv_filename = f"{filename}.csv"
# Write the data to the CSV file
with open(csv_filename, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
# Write headers first (if any)
if headers:
headers.append("Date") # Add 'Date' as the last header
writer.writerow(headers)
# Write table rows
writer.writerows(table_data)
print(f"Data has been written to {csv_filename} from {url}")
# Read URLs from standard input (stdin)
def read_urls_from_stdin():
urls = sys.stdin.read().splitlines()
return urls
# Main function to process each URL
def main():
urls = read_urls_from_stdin()
if not urls:
print("No URLs provided.")
return
for url in urls:
scrape_table_to_csv(url)
# Run the script
if __name__ == "__main__":
main()
Explanation of Changes:
extract_date_from_page(content):
The new regular expression r'\b([A-Za-z]{3})\s*(\d{1,2}),\s*(\d{4})\b' is designed to match dates in the format:
A three-letter month abbreviation ([A-Za-z]{3}).
A day number (\d{1,2}).
A comma and a four-digit year (\d{4}).
Example match: Apr 23, 2019.
How it Works:
The extract_date_from_page function scans the page content for the first occurrence of a date matching this format.
If found, it returns the date (e.g., Apr 23, 2019); otherwise, it returns "Unknown Date".
Adding the Date to Each Row:
After extracting the date from the page, it is appended as the last field to each row of the table data.
The headers are also updated to include a "Date" column.
Example:
Given the following table on the webpage:
Name Value
Item 1 $200 (5)
Item 2 $150 (10.5)
Item 3 $300
And a date found on the page like Apr 23, 2019, the output CSV will look like this:
Name Value Date
Item 1 $200 Apr 23, 2019
Item 2 $150 Apr 23, 2019
Item 3 $300 Apr 23, 2019
How to Run:
With Input from a File: You can provide URLs through a file (e.g., urls.txt), and then run the script with:
python scrape_to_csv.py < urls.txt
With URLs Directly from stdin: Alternatively, you can pipe URLs directly to the script:
echo -e "https://example.com/page1\nhttps://example.com/page2" | python scrape_to_csv.py
Notes:
Date Formats: This script is designed specifically for dates in the format Mon dd, yyyy (e.g., Apr 23, 2019). If you need to handle other formats, you can adjust the regular expression accordingly.
Error Handling: If no table or date is found, the script will print a message and continue processing the next URL.
Get smarter responses, upload files and images, and more.
ChatGPT can make mistakes. Check important info.