changedotorgscraper 0.0.2__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.
- ChangeDotOrgScraper/ChangeDotOrgScraper.py +185 -0
- ChangeDotOrgScraper/__init__.py +1 -0
- ChangeDotOrgScraper-0.0.2.dist-info/LICENSE +21 -0
- ChangeDotOrgScraper-0.0.2.dist-info/METADATA +132 -0
- ChangeDotOrgScraper-0.0.2.dist-info/RECORD +7 -0
- ChangeDotOrgScraper-0.0.2.dist-info/WHEEL +5 -0
- ChangeDotOrgScraper-0.0.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
from bs4 import BeautifulSoup
|
|
3
|
+
import json
|
|
4
|
+
from tqdm import tqdm
|
|
5
|
+
import re
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import ast
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def get_soup(url):
|
|
11
|
+
'''
|
|
12
|
+
Gets BeautifulSoup object from the url provided by the user.
|
|
13
|
+
|
|
14
|
+
Paramters:
|
|
15
|
+
- url (str): url link provided by the user
|
|
16
|
+
|
|
17
|
+
Returns:
|
|
18
|
+
- soup (BeautifulSoup obj): A beautiful object of the url provided by the user.
|
|
19
|
+
'''
|
|
20
|
+
if url.endswith("&offset=0"):
|
|
21
|
+
url=url[:url.find("offset=")]
|
|
22
|
+
url=url+"offset="
|
|
23
|
+
elif url.endswith("&offset="):
|
|
24
|
+
url=url
|
|
25
|
+
else:
|
|
26
|
+
url=url+"&offset="
|
|
27
|
+
|
|
28
|
+
# Send a GET request to the URL
|
|
29
|
+
response = requests.get(url+"0")
|
|
30
|
+
try:
|
|
31
|
+
# Parse the HTML content of the page with Beautiful Soup
|
|
32
|
+
soup = BeautifulSoup(response.text, 'html.parser')
|
|
33
|
+
except:
|
|
34
|
+
print("Please check the URL or change.org may have been updated such that this package is no long compatable")
|
|
35
|
+
return(soup)
|
|
36
|
+
|
|
37
|
+
def get_pages_no(soup):
|
|
38
|
+
'''
|
|
39
|
+
Determines the number of pages of worth of petitions within the change.org search.
|
|
40
|
+
|
|
41
|
+
Parameters:
|
|
42
|
+
- soup (BeautifulSoup obj): A beautiful object of the existing page of in the change.org petition of interests
|
|
43
|
+
|
|
44
|
+
Returns:
|
|
45
|
+
- offset_nos (list): list of page numbers.
|
|
46
|
+
'''
|
|
47
|
+
match = re.search(r'<div class="corgi-1weo53w">([\d,]+) results</div>', str(soup))
|
|
48
|
+
|
|
49
|
+
if match:
|
|
50
|
+
# Extract the matched group which contains the number of results
|
|
51
|
+
num_results = match.group(1)
|
|
52
|
+
# Optional: convert the string number to an integer, removing commas
|
|
53
|
+
num_results_int = int(num_results.replace(',', ''))
|
|
54
|
+
#print(num_results) # This will print the string
|
|
55
|
+
#print(num_results_int) # This will print the integer
|
|
56
|
+
else:
|
|
57
|
+
print("No match found")
|
|
58
|
+
offset=(num_results_int//10)*10
|
|
59
|
+
offset_nos=[i for i in range(0,offset+10,10)]
|
|
60
|
+
return(offset_nos)
|
|
61
|
+
|
|
62
|
+
def get_current_page(soup):
|
|
63
|
+
'''
|
|
64
|
+
Extracts all the petition and the corresponding creator information from the beautiful soup object.
|
|
65
|
+
|
|
66
|
+
Parameters:
|
|
67
|
+
- soup (BeautifulSoup obj): A beautiful object of the existing page of in the change.org petition of interests
|
|
68
|
+
|
|
69
|
+
Returns:
|
|
70
|
+
- petition_info (list): a list of dictionaries containing all the information of the petitions within the existing change.org page.
|
|
71
|
+
- creator_info (list): a list of dictionaries containing all the information of the corrresponding creators among the petitions within the existing change.org page.
|
|
72
|
+
'''
|
|
73
|
+
filter_soup=soup.find('script', text=lambda t: '__HYDRATION_DATA__' in t)
|
|
74
|
+
filter_soup=str(filter_soup)
|
|
75
|
+
start_str_pos=filter_soup.find("\"prefetchedData\":{\"")
|
|
76
|
+
end_str_pos=soup.find("}}}}")
|
|
77
|
+
info=filter_soup[start_str_pos:end_str_pos]
|
|
78
|
+
info=info.replace('</script>', '')
|
|
79
|
+
info=info.replace('<script>', '')
|
|
80
|
+
info=info.replace('__HYDRATION_DATA__=',"")
|
|
81
|
+
|
|
82
|
+
pattern = r'\{"petition":\{"__typename":"Petition".*?"highlight"'
|
|
83
|
+
|
|
84
|
+
# Find all instances in the text
|
|
85
|
+
matches = re.findall(pattern, info, re.DOTALL)
|
|
86
|
+
|
|
87
|
+
# Print matches
|
|
88
|
+
lists_of_petitions=[]
|
|
89
|
+
for match in (matches):
|
|
90
|
+
lists_of_petitions.append(match)
|
|
91
|
+
petition_info,creator_info=[],[]
|
|
92
|
+
petition_info_pattern = r'\{"petition":\{"__typename":"Petition".*?\}\}'
|
|
93
|
+
creator_info_pattern = r'"slug".*?"highlight"'
|
|
94
|
+
|
|
95
|
+
for i in lists_of_petitions:
|
|
96
|
+
petition_info.append(re.findall(petition_info_pattern, i, re.DOTALL))
|
|
97
|
+
creator_info.append(re.findall(creator_info_pattern, i, re.DOTALL))
|
|
98
|
+
petition_info=[item for sublist in petition_info for item in sublist]
|
|
99
|
+
creator_info=[item for sublist in creator_info for item in sublist]
|
|
100
|
+
|
|
101
|
+
for i in (range(len(lists_of_petitions))):
|
|
102
|
+
petition_info[i]=petition_info[i].replace('false', 'False').replace('true', 'True').replace('null', 'None')
|
|
103
|
+
petition_info[i]=ast.literal_eval(petition_info[i]+"}}")
|
|
104
|
+
creator_info[i]=creator_info[i].replace(",\"highlight\"","")
|
|
105
|
+
creator_info[i]="{"+creator_info[i]
|
|
106
|
+
# print(creator_info[i])
|
|
107
|
+
creator_info[i]= ast.literal_eval(creator_info[i].replace('false', 'False').replace('true', 'True').replace('null', 'None'))
|
|
108
|
+
return(petition_info,creator_info)
|
|
109
|
+
|
|
110
|
+
def clean_info(petition_info, creator_info):
|
|
111
|
+
'''
|
|
112
|
+
Organizes and parses all each petition and creator information
|
|
113
|
+
|
|
114
|
+
Parameters:
|
|
115
|
+
- petition_info (list): a list of dictionaries of petitions with informations of all the petitions within a change.org page.
|
|
116
|
+
Each dictionary contains information for each petition, which was extracted from `get_current_page` function.
|
|
117
|
+
- creator_info (list): a list of dictionaries with creator information from the corresponding petition in petition_info.
|
|
118
|
+
Each dictionary contains information for each creator of each corresponding petition, which was extracted from `get_current_page` function.
|
|
119
|
+
|
|
120
|
+
Returns
|
|
121
|
+
list_of_petitions (list): a list of dictionaries, with each dictionary containing information about the petition and creator from the list of petitions within one webpage.
|
|
122
|
+
These includes petition title, Description, target audience, signature count, creator name, date created, location createdd, and victory status.
|
|
123
|
+
'''
|
|
124
|
+
list_of_petitions=[]
|
|
125
|
+
for i,j in zip(petition_info,creator_info):
|
|
126
|
+
petition_title=i["petition"]["ask"]
|
|
127
|
+
petition_description=i["petition"]["description"]
|
|
128
|
+
petition_target=i["petition"]["targetingDescription"]
|
|
129
|
+
petition_signatures=i["petition"]["signatureState"]["signatureCount"]["displayed"]
|
|
130
|
+
petition_creator=j["user"]["displayName"]
|
|
131
|
+
date_created=j['createdAt']
|
|
132
|
+
location=j["user"]["formattedLocationString"]
|
|
133
|
+
victory_verification_status=j['isVerifiedVictory']
|
|
134
|
+
curr={"Petition title":petition_title,
|
|
135
|
+
"Description":petition_description,
|
|
136
|
+
"target":petition_target,
|
|
137
|
+
"signature count":petition_signatures,
|
|
138
|
+
"creator":petition_creator,
|
|
139
|
+
"date created":date_created,
|
|
140
|
+
"location created":location,
|
|
141
|
+
"Victory verification status":victory_verification_status}
|
|
142
|
+
list_of_petitions.append(curr)
|
|
143
|
+
return(list_of_petitions)
|
|
144
|
+
|
|
145
|
+
def scrape_petitions(url):
|
|
146
|
+
'''
|
|
147
|
+
Scrapes all the petition and its corresponding details based on the url provided.
|
|
148
|
+
This encompasses going looping through all the pages automatically to extract ALL the petitions, not just the petitions listed within the page itself.
|
|
149
|
+
|
|
150
|
+
Parameters:
|
|
151
|
+
- url (str): url from change.org upon searching for the list of petitions
|
|
152
|
+
|
|
153
|
+
Returns:
|
|
154
|
+
- data_of_petitions (pandas dataframe): dataset of petitions with all the relevant information, including
|
|
155
|
+
petition title, Description, target audience, signature count, creator name, date created, location createdd, and victory status.
|
|
156
|
+
'''
|
|
157
|
+
soup=get_soup(url)
|
|
158
|
+
# gets the list of page numbers.
|
|
159
|
+
pages=get_pages_no(soup)
|
|
160
|
+
list_of_petitions=[]
|
|
161
|
+
if url.endswith("&offset=0"):
|
|
162
|
+
url=url[:url.find("offset=")]
|
|
163
|
+
url=url+"offset="
|
|
164
|
+
elif url.endswith("&offset="):
|
|
165
|
+
url=url
|
|
166
|
+
else:
|
|
167
|
+
url=url+"&offset="
|
|
168
|
+
# this for loop runs through all the pages and extracts the petitions.
|
|
169
|
+
for (i) in tqdm(pages):
|
|
170
|
+
#print(f"obtaining page {i} of {pages[-1]} pages")
|
|
171
|
+
i=str(i)
|
|
172
|
+
response = requests.get(url+i)
|
|
173
|
+
try:
|
|
174
|
+
# Parse the HTML content of the page with Beautiful Soup
|
|
175
|
+
soup = BeautifulSoup(response.text, 'html.parser')
|
|
176
|
+
except:
|
|
177
|
+
print("Please check the URL or change.org may have been updated such that this package is no long compatable")
|
|
178
|
+
petition_info,creator_info=get_current_page(soup)
|
|
179
|
+
list_of_petitions+=clean_info(petition_info, creator_info)
|
|
180
|
+
data_of_petitions=pd.DataFrame(list_of_petitions)
|
|
181
|
+
return(data_of_petitions)
|
|
182
|
+
|
|
183
|
+
# example use case
|
|
184
|
+
# url = "https://www.change.org/search?q=Supplemental%20Nutrition%20Assistance%20Program&offset=0"
|
|
185
|
+
# SNAP_petitions=scrape_petitions(url)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .ChangeDotOrgScraper import scrape_petitions
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Charles Alba
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: ChangeDotOrgScraper
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Summary: Scrapes petitions and corresponding information from change.org
|
|
5
|
+
Author: Charles Alba
|
|
6
|
+
Author-email: alba@wustl.edu
|
|
7
|
+
License: MIT
|
|
8
|
+
Keywords: change.org,petitions,web scraping
|
|
9
|
+
Classifier: Development Status :: 1 - Planning
|
|
10
|
+
Classifier: Intended Audience :: Education
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Operating System :: Unix
|
|
14
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
15
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: requests
|
|
19
|
+
Requires-Dist: bs4
|
|
20
|
+
Requires-Dist: tqdm
|
|
21
|
+
Requires-Dist: pandas
|
|
22
|
+
Requires-Dist: ast
|
|
23
|
+
|
|
24
|
+
Scrapes petitions from [change.org](https://www.change.org/).
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
[](https://pypi.org/project/ChangeDotOrgScraper/)[](https://github.com/cja5553/change_org_scraper)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
## Quick Start
|
|
33
|
+
|
|
34
|
+
Here we provide a quick example on how you can use `ChangeDotOrgScraper` to scrape petitions right from [change.org](https://www.change.org/).
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
|
|
40
|
+
from ChangeDotOrgScraper import scrape_petitions
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
# Specify the url of your scrape.org searches
|
|
45
|
+
|
|
46
|
+
## for instance, we shall scrape petitions from the search "supplemental nutrition assistance program"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
url='https://www.change.org/search?q=Supplemental%20Nutrition%20Assistance%20Program&offset=0'
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
# scrape the petition data from change.org
|
|
55
|
+
|
|
56
|
+
SNAP_petitions_df=scrape_petitions(url)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
print(SNAP_petitions_df)
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
It returns a dataframe with all the petitions related to SNAP as well as corresponding details (eg description, date created, target audience signature count, location created, victory status, etc).
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
## Requirements
|
|
73
|
+
|
|
74
|
+
### Required packages
|
|
75
|
+
|
|
76
|
+
To use `ChangeDotOrgScraper`, you are required to have the following packages installed:
|
|
77
|
+
|
|
78
|
+
- `requests`
|
|
79
|
+
|
|
80
|
+
- `bs4`
|
|
81
|
+
|
|
82
|
+
- `tqdm`
|
|
83
|
+
|
|
84
|
+
- `pandas`
|
|
85
|
+
|
|
86
|
+
- `ast`
|
|
87
|
+
|
|
88
|
+
- `re` (Python standard library)
|
|
89
|
+
|
|
90
|
+
- `json` (Python standard library)
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
If you do not have these packages installed in python, you can do the following:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
|
|
98
|
+
pip install requests bs4 tqdm pandas ast
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
## Installation
|
|
105
|
+
|
|
106
|
+
To install in python, simply do the following:
|
|
107
|
+
|
|
108
|
+
```bash
|
|
109
|
+
|
|
110
|
+
pip install ChangeDotOrgScraper
|
|
111
|
+
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
## Citation
|
|
117
|
+
|
|
118
|
+
This package was made as part of a larger study on food stamps (i.e., SNAP), therefore, we do not have the paper ready just yet.
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
However, if you wish to cite, you may cite:
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
C Alba (2024) `ChangeDotOrgScraper`: A python package to scrape petitions from change.org. PyPi.
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
# Questions?
|
|
131
|
+
|
|
132
|
+
Contact me at [alba@wusl.edu](mailto:alba@wusl.edu)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
ChangeDotOrgScraper/ChangeDotOrgScraper.py,sha256=R5HNEdgpU97A3vU2vpOVMcjQWXPtRixo-IQ-mB-vVqI,8187
|
|
2
|
+
ChangeDotOrgScraper/__init__.py,sha256=udFyUF8Cj1rykEfzXJHL_I5TCAVCKIuaB1aVQ5Q_piY,49
|
|
3
|
+
ChangeDotOrgScraper-0.0.2.dist-info/LICENSE,sha256=e4pyZPbTvA9TCeikHkTvEIspfgFWVBy0SXBirQcd5zI,1090
|
|
4
|
+
ChangeDotOrgScraper-0.0.2.dist-info/METADATA,sha256=oxgSvK4887BHrzUGS8HYFtCmsAfKZF2druX14a91m8s,2914
|
|
5
|
+
ChangeDotOrgScraper-0.0.2.dist-info/WHEEL,sha256=Z4pYXqR_rTB7OWNDYFOm1qRk0RX6GFP2o8LgvP453Hk,91
|
|
6
|
+
ChangeDotOrgScraper-0.0.2.dist-info/top_level.txt,sha256=tFjbf4MGx-tBkBXo17h0GbnBJIfXCRMODnBgHSxN4qY,20
|
|
7
|
+
ChangeDotOrgScraper-0.0.2.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ChangeDotOrgScraper
|