Compare commits
8 Commits
c246a09b89
...
0c3cfdf0e9
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c3cfdf0e9 | |||
| 8b837b2d7c | |||
| d0c541a318 | |||
| bc1619f11e | |||
| e2fe8ab8e8 | |||
| 0627bd446b | |||
| 96d8926ac8 | |||
| 3595537ed1 |
BIN
chromedriver
Executable file
BIN
chromedriver
Executable file
Binary file not shown.
53
main.py
Normal file
53
main.py
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import requests
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
from urllib.parse import urlparse, urlunparse
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
|
def scrape_headings(url):
|
||||||
|
try:
|
||||||
|
# Check if the URL has a scheme (http/https), and add one if missing
|
||||||
|
parsed_url = urlparse(url)
|
||||||
|
if not parsed_url.scheme:
|
||||||
|
url = urlunparse(('http',) + parsed_url[1:])
|
||||||
|
|
||||||
|
# Send an HTTP GET request to the specified URL
|
||||||
|
response = requests.get(url)
|
||||||
|
|
||||||
|
# Check if the request was successful
|
||||||
|
if response.status_code == 200:
|
||||||
|
# Parse the HTML content using BeautifulSoup
|
||||||
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
|
|
||||||
|
# Find all the heading elements (h1, h2, h3, etc.)
|
||||||
|
headings = soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
|
||||||
|
|
||||||
|
# Extract and put to csv the result of headings
|
||||||
|
heading_list = [heading.text for heading in headings]
|
||||||
|
df = pd.DataFrame(heading_list)
|
||||||
|
df.to_csv('output.csv')
|
||||||
|
|
||||||
|
else:
|
||||||
|
print(f"Failed to retrieve content from {url}. Status code: {response.status_code}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
print(f"An error occurred: {str(e)}")
|
||||||
|
|
||||||
|
|
||||||
|
# To check if to do another search for URL
|
||||||
|
def main_start():
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
url = input("Enter the URL: ")
|
||||||
|
scrape_headings(url)
|
||||||
|
# df = pd.DataFrame(scrape_headings(url))
|
||||||
|
search_again = input("Do you want to search again? y/n:").lower()
|
||||||
|
if search_again == 'y':
|
||||||
|
# df.to_csv('output.csv')
|
||||||
|
main_start()
|
||||||
|
else:
|
||||||
|
# df.to_csv('output.csv')
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
main_start()
|
||||||
|
|
||||||
Reference in New Issue
Block a user