-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathwords.py
More file actions
57 lines (40 loc) · 1.24 KB
/
Copy pathwords.py
File metadata and controls
57 lines (40 loc) · 1.24 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
"""Reterive and print words from a URL.
Usage:
python3 words.py <URL>
Example: python words.py http://sixty-north.com/c/t.txt
"""
import requests
import sys
def fetch_words(url):
"""Fetch a list of string from URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing words from the document.
"""
user_agents = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
headers = {'User-Agents':user_agents}
response = requests.get(url,headers = headers)
story_word = []
for line in response.text.split('\n'):
line_word = line.split()
for word in line_word:
story_word.append(word)
return story_word
def print_words(story_word):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for word in story_word:
print(word)
def main(url):
"""Print each word from a text document from at a URL.
Args:
url: The URL of a UTF-8 text document.
"""
words = fetch_words(url)
print_words(words)
if __name__ == "__main__":
url = sys.argv[1] # The 0th arg is the module filename.
main(url)