pywebscrapr 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ import os
2
+ import csv
3
+ import requests
4
+ from bs4 import BeautifulSoup
5
+ from urllib.parse import urlparse, urljoin
6
+
7
+ def _get_absolute_url(base_url, relative_url):
8
+ """Helper function to get absolute URLs."""
9
+ return urljoin(base_url, relative_url)
10
+
11
+ def _download_image(url, save_path):
12
+ """Helper function to download an image."""
13
+ response = requests.get(url)
14
+ with open(save_path, 'wb') as file:
15
+ file.write(response.content)
16
+
17
+ def scrape_images(links_file=None, links_array=None, save_folder='images'):
18
+ """
19
+ Scrape image content from the given links and save to specified output folder.
20
+
21
+ Parameters:
22
+ - links_file (str): Path to a file containing links, with each link on a new line.
23
+ - links_array (list): List of links to scrape text from.
24
+ - output_folder (str): Folder to save the scraped images.
25
+
26
+ Example:
27
+ ```python
28
+ from pywebscrapr import scrape_images
29
+
30
+ # Using links from a file and saving images to output_images folder.
31
+ scrape_text(links_file='links.txt', output_folder='output_images')
32
+ ```
33
+ """
34
+ if not os.path.exists(save_folder):
35
+ os.makedirs(save_folder)
36
+
37
+ links = []
38
+ if links_file:
39
+ with open(links_file, 'r') as file:
40
+ links = file.read().splitlines()
41
+ elif links_array:
42
+ links = links_array
43
+ else:
44
+ raise ValueError("Either 'links_file' or 'links_array' must be provided.")
45
+
46
+ for link in links:
47
+ response = requests.get(link)
48
+ soup = BeautifulSoup(response.text, 'html.parser')
49
+
50
+ for img_tag in soup.find_all('img'):
51
+ img_url = _get_absolute_url(link, img_tag.get('src'))
52
+
53
+ # Skip data URLs
54
+ if not img_url.startswith('data:'):
55
+ img_name = os.path.basename(urlparse(img_url).path)
56
+ save_path = os.path.join(save_folder, img_name)
57
+ _download_image(img_url, save_path)
58
+
59
+ def scrape_text(links_file=None, links_array=None, output_file='output.txt', csv_output_file=None, remove_extra_whitespace=True):
60
+ """
61
+ Scrape textual content from the given links and save to specified output file(s).
62
+
63
+ Parameters:
64
+ - links_file (str): Path to a file containing links, with each link on a new line.
65
+ - links_array (list): List of links to scrape text from.
66
+ - output_file (str): File to save the scraped text.
67
+ - csv_output_file (str): File to save the URL and text information in CSV format.
68
+ - remove_extra_whitespace (bool): If True, remove extra whitespace and empty lines from the output.
69
+
70
+ Example:
71
+ ```python
72
+ from pywebscrapr import scrape_text
73
+
74
+ # Using links from a file and saving text to output.txt
75
+ scrape_text(links_file='links.txt', output_file='output.txt')
76
+
77
+ # Using links directly and saving text to output.txt and csv_output.csv with extra whitespace removal
78
+ links = ['https://example.com/page1', 'https://example.com/page2']
79
+ scrape_text(links_array=links, output_file='output.txt', csv_output_file='csv_output.csv', remove_extra_whitespace=True)
80
+ ```
81
+ """
82
+ links = []
83
+ if links_file:
84
+ with open(links_file, 'r') as file:
85
+ links = file.read().splitlines()
86
+ elif links_array:
87
+ links = links_array
88
+ else:
89
+ raise ValueError("Either 'links_file' or 'links_array' must be provided.")
90
+
91
+ all_text = ""
92
+ csv_data = []
93
+
94
+ for link in links:
95
+ response = requests.get(link)
96
+ soup = BeautifulSoup(response.text, 'html.parser')
97
+
98
+ for element in soup.find_all(lambda tag: tag.name not in ['script', 'style']):
99
+ if remove_extra_whitespace:
100
+ text = element.get_text(strip=True) # Remove extra whitespace
101
+ if text: # Skip empty lines
102
+ all_text += text + "\n"
103
+ else:
104
+ text = element.get_text()
105
+ all_text += text + "\n"
106
+ if csv_output_file:
107
+ csv_data.append({'URL': link, 'Text': text})
108
+
109
+ # Save text to output file
110
+ with open(output_file, 'w', encoding='utf-8') as text_file:
111
+ if remove_extra_whitespace:
112
+ text_file.write(all_text.rstrip()) # Remove trailing whitespace
113
+ else:
114
+ text_file.write(all_text)
115
+
116
+ # Save CSV data to CSV file
117
+ if csv_output_file:
118
+ with open(csv_output_file, 'w', newline='', encoding='utf-8') as csv_file:
119
+ fieldnames = ['URL', 'Text']
120
+ writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
121
+ writer.writeheader()
122
+ writer.writerows(csv_data)
@@ -0,0 +1,7 @@
1
+ MIT License (Modified)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to make derivative works based on the Software, provided that any substantial changes to the Software are clearly distinguished from the original work and are distributed under a different name.
4
+
5
+ The original copyright notice and disclaimer must be retained in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS," WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.1
2
+ Name: pywebscrapr
3
+ Version: 0.1.0
4
+ Summary: An open-source Python library for web scraping tasks.
5
+ Home-page: https://github.com/infinitode/pywebscrapr
6
+ Author: Infinitode Pty Ltd
7
+ Author-email: infinitode.ltd@gmail.com
8
+ Classifier: Development Status :: 3 - Alpha
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3.6
12
+ Classifier: Programming Language :: Python :: 3.7
13
+ Classifier: Programming Language :: Python :: 3.8
14
+ Classifier: Programming Language :: Python :: 3.9
15
+ Classifier: Programming Language :: Python :: 3.10
16
+ Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Python: >=3.6
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ Requires-Dist: requests
22
+ Requires-Dist: beautifulsoup4
23
+
24
+ An open-source Python library for web scraping tasks. Includes support for both image scraping and text scraping.
@@ -0,0 +1,6 @@
1
+ pywebscrapr/__init__.py,sha256=cVYuZOKDc1-6HPbkWcWc_wR0lvA3MZv2To_prUZuGAU,4552
2
+ pywebscrapr-0.1.0.dist-info/LICENSE,sha256=ZIftxnYzzlrappVuiyXoFI6QAiB1s2fzg9w07mxF-QI,949
3
+ pywebscrapr-0.1.0.dist-info/METADATA,sha256=2BA3XAFDW-1isEB9sNEIcgR903Mli0gHTekHxDIPjRE,983
4
+ pywebscrapr-0.1.0.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
5
+ pywebscrapr-0.1.0.dist-info/top_level.txt,sha256=-3_Ebeblzcx7vsig3hq8MJ43Zy_iBXFcge2NaOvaPLE,12
6
+ pywebscrapr-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.42.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ pywebscrapr