-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualMajor.py
70 lines (59 loc) · 2.68 KB
/
visualMajor.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
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
from bs4 import BeautifulSoup
import csv
from selenium import webdriver
import visualMinor
from visualMinor import postWriter, postLink
driver = webdriver.Chrome('/opt/google/chrome/chromedriver')
with open('result.csv', 'w', newline='') as f:
thewriter = csv.writer(f)
thewriter.writerow(['Title', 'Date', 'Author', 'Link'])
def resultWriter(title=None, date=None, author=None, link=None):
with open('result.csv', 'a', newline='') as f:
fieldnames = ['Title', 'Date', 'Author', 'Link']
thewriter = csv.DictWriter(f, fieldnames=fieldnames)
thewriter.writerow({'Title' : title, 'Date' : date, 'Author' : author, 'Link' : link})
def baseData(url):
driver.get(url)
r = driver.page_source
soup = BeautifulSoup(r, 'html.parser')
for post_header in soup.find_all("div", class_="post-header"):
title = post_header.a.string
link = post_header.a['href']
date = post_header.div.span.a.string
post_author = post_header.div
author = post_author.find('span', class_='author').a.string
print("Title : " + title)
print("Posted on: " + date)
print("Posted by: " + author)
print("Post Link: " + link)
print("\n----------------------------------------------------\n")
resultWriter(title, date, author, link)
def nextPageCheck(url):
driver.get(url)
r = driver.page_source
soup = BeautifulSoup(r, 'html.parser')
if "OLDER POST" in str(soup):
older_post = soup.find('a', class_='next-posts-link')['href']
return older_post, True
else:
return False
def visual():
url = 'https://blog.scrapinghub.com/'
while True:
if nextPageCheck(url):
url = nextPageCheck(url)[0]
print("+---------------------------------------------------------------+")
print("| |")
print(f" Going to next page: {url} ")
print("| |")
print("+---------------------------------------------------------------+")
baseData(url)
else:
print("+---------------------------------------------------------------+")
print("| |")
print("| No more page left. Collecting post data |")
print("| |")
print("+---------------------------------------------------------------+")
driver.quit()
visualMinor.postWriter()
break