-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.py
More file actions
62 lines (57 loc) · 2 KB
/
Copy pathscraper.py
File metadata and controls
62 lines (57 loc) · 2 KB
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
"""
scrape blogs from the web and save them
"""
# from urllib.request import urlopen
# from urllib.error import HTTPError
import time
import os
from bs4 import BeautifulSoup
import requests
# def getLinks(url):
# try:
# html = urlopen(url)
# except HTTPError as e:
# print(e)
# return "error occured"
# else:
# newObj = BeautifulSoup(html, "html.parser")
# if newObj is not None:
# links = []
# for link in newObj.findAll("a"):
# links.append(link)
# return links
# else:
# return "error occured"
# def getExtLinks(bsObj, excludeURL):
# externalLinks = set()
# for link in bsObj.findAll(
# "a", href=re.compile("^(http|www)((?!" + excludeURL + ").)*$")):
# if link.attrs['href'] not in externalLinks:
# externalLinks.add(link.attrs['href'])
# return externalLinks
# after update, emacs flycheck becomes rather aggressive, cammelCase is regarded as improper
def get_rec10(userid):
"""
accepts a username and returns ten recent articles of that user in the
form of BeautifulSoup objects
"""
url = "https://medium.com/@" + userid + "/latest"
user_page = requests.get(url)
soup = BeautifulSoup(user_page.content, "html5lib")
articles = []
for div in soup.find_all('div', {'class', 'layoutSingleColumn'}):
link = div.find('a')['href']
time.sleep(10) #sleep for a while to avoid being banned
articles.append(BeautifulSoup(requests.get(link).content, "html5lib"))
# process soup objects
outpath = os.path.join('./', userid)
if not os.path.exists(outpath):
os.makedirs(outpath)
i = 0
for article in articles:
i += 1
fout = open(os.path.join(outpath, 'article' + str(i) + '.txt'), 'w')
for passage in article.find_all('div', {'class', 'section-inner'}):
for paragraph in passage.find_all('p'):
fout.write(paragraph.text)
fout.close()