-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrapperMajor.py
64 lines (55 loc) · 2.56 KB
/
scrapperMajor.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
import requests
from bs4 import BeautifulSoup
import csv
import scrapperMinor
from scrapperMinor import postWriter, postLink
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):
r = requests.get(url).text
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):
r = requests.get(url).text
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 headless():
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("+---------------------------------------------------------------+")
scrapperMinor.postWriter()
break