forked from rhakhshchasch/python-scraping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress_plugin_scraper.py
41 lines (33 loc) · 1.15 KB
/
wordpress_plugin_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
import requests
from bs4 import BeautifulSoup as BS
import csv
def get_html(url: str) -> str:
return requests.get(url).text
def clean_data(string: str):
return string.replace(",", "").replace("total ratings", "").strip()
def write_csv(data):
with open('plugins.csv', 'a', newline='') as f:
writer = csv.writer(f)
writer.writerow((
data['name'],
data['url'],
data['reviews']
))
def get_data(html: str) -> None:
soup = BS(html, 'lxml')
get_plug = soup.find_all(class_='plugin-section')[3]
plugins = get_plug.find_all('article')
for plugin in plugins:
name = plugin.find('h3').text
url = plugin.find('h3').find('a').get('href')
rating = plugin.find('span', class_='rating-count').find('a').text
clean_rating = clean_data(rating)
data = {'name': name,
'url': url,
'reviews': clean_rating}
write_csv(data)
def run() -> None:
url = "https://wordpress.org/plugins/"
get_data(get_html(url))
if __name__ == '__main__':
run()