1

00:00:00,270  -->  00:00:07,170
Welcome to lesson 38 which roughly covers pages 233 236 of the automate the boring stuff with Python

2

00:00:07,170  -->  00:00:15,090
textbook in this lesson we're going to talk about the web browser module and the web browser modules

3

00:00:15,180  -->  00:00:20,550
open function can launch a new browser to a specified or else let's enter the following into the interactive

4

00:00:20,550  -->  00:00:30,330
shell web browser open and then pass that a string of A you are able to open automate the boring stuff

5

00:00:30,670  -->  00:00:31,530
dot com

6

00:00:34,350  -->  00:00:37,320
and you can see Python will launch a new browser.

7

00:00:37,530  -->  00:00:44,310
Your operating system Stiefel browser whatever that's configured to to the Web site that you passed

8

00:00:44,310  -->  00:00:45,110
it.

9

00:00:45,130  -->  00:00:49,850
Know this is about the only thing that the web browser module can do this open function.

10

00:00:49,980  -->  00:00:55,710
But even so the open function does make some interesting things possible for example it's really tedious

11

00:00:55,710  -->  00:01:03,150
to go to some web site and then have to copy by pressing Control-C some address and then opening a new

12

00:01:03,150  -->  00:01:10,230
tab and going to map start Google dot com and then having to paste this into that field and then press

13

00:01:10,230  -->  00:01:11,230
enter.

14

00:01:11,670  -->  00:01:17,040
Just to bring up a map of that address and we can take a few steps out of this process by writing a

15

00:01:17,040  -->  00:01:22,890
simple script that will automatically automatically launch the browser to this address on the Google

16

00:01:22,890  -->  00:01:24,840
map site.

17

00:01:25,230  -->  00:01:30,390
So let's start up a new file editor window by clicking on file new file.

18

00:01:30,510  -->  00:01:35,880
And let's see we're going to need to import the web browser module for this but let's think about what

19

00:01:35,880  -->  00:01:38,370
exactly we want this program to do.

20

00:01:38,370  -->  00:01:44,300
Now ideally would be nice on Windows if I could just hit windows key are to bring up the Run dialog

21

00:01:44,370  -->  00:01:48,250
and then type out map it to run the map it program.

22

00:01:48,270  -->  00:01:52,260
Well just let me just go ahead and save this as.

23

00:01:52,260  -->  00:01:52,960
Map it.

24

00:01:52,980  -->  00:01:54,620
P y.

25

00:01:54,630  -->  00:02:05,180
Let's go to the my users slash slash my Python scripts folder that we set up in a previous lesson.

26

00:02:05,310  -->  00:02:13,020
I'll just save this as Mattituck it Type-I So be nice if we could just run this program by hitting Windows

27

00:02:13,020  -->  00:02:14,490
key are taking out.

28

00:02:14,490  -->  00:02:21,720
Map it and then just pressing OK and then having having the python script read the contents of the clipboard

29

00:02:21,750  -->  00:02:25,730
and opening up a web browser to the Google Maps page at that address.

30

00:02:25,740  -->  00:02:31,980
Alternatively we can also have is that when you're typing it out right here take out the address that

31

00:02:31,980  -->  00:02:38,280
you want and these command line arguments will be read by the map it program and it would open up the

32

00:02:38,310  -->  00:02:41,400
Google Maps Web site to this address.

33

00:02:41,550  -->  00:02:46,680
So that'll use the command line arguments which we went over in a previous lesson remember that the

34

00:02:46,680  -->  00:02:52,110
command line arguments means we need to import the CIS module because the command line arguments are

35

00:02:52,110  -->  00:02:59,250
stored in a list variable because the command line arguments are stored in the list in the sys dot argv

36

00:03:00,060  -->  00:03:01,530
variable.

37

00:03:02,340  -->  00:03:09,990
So if I ran something like map it up PI 870 Valencias street

38

00:03:12,950  -->  00:03:18,690
the R v variable would contain a value that looks something like this would have map it and then hit

39

00:03:18,790  -->  00:03:19,570
70.

40

00:03:19,770  -->  00:03:21,690
It's delimited by spaces.

41

00:03:21,960  -->  00:03:24,760
So these command line arguments would all be separate.

42

00:03:24,900  -->  00:03:28,370
The next string would be Valencia and that final string would be Street.

43

00:03:28,980  -->  00:03:37,710
So first let's have our script check if command line arguments were passed.

44

00:03:37,710  -->  00:03:44,540
So since this is a list value just like any other list value we can call we can pass it to Len.

45

00:03:44,970  -->  00:03:47,140
The length function.

46

00:03:47,670  -->  00:03:52,770
And if we didn't call it with any of these command line arguments the length of this list would just

47

00:03:52,770  -->  00:03:53,780
be one.

48

00:03:53,800  -->  00:03:59,310
So we know that if there are command line arguments this list will have a length of greater than 1.

49

00:03:59,310  -->  00:04:05,070
So we'll just say if the length of the system or the list is greater than one then we know command line

50

00:04:05,070  -->  00:04:09,730
arguments have been passed to it.

51

00:04:09,780  -->  00:04:15,340
So again we have this list here which is going to be inside of our V.

52

00:04:16,040  -->  00:04:21,000
We want to do is take these strings right here all the strings after the first one and combine them

53

00:04:21,000  -->  00:04:26,560
into a single string that looks like this.

54

00:04:27,100  -->  00:04:33,390
And if you remember this is exactly what the join method does for strings so you get have a string with

55

00:04:33,390  -->  00:04:39,030
a single space in it and called join and then pass it that list argv.

56

00:04:39,160  -->  00:04:41,200
So we don't want this first part here.

57

00:04:41,280  -->  00:04:46,510
We want to get a slice that is create a new list with just these values.

58

00:04:46,620  -->  00:04:50,070
So we have to have these square brackets and a colon.

59

00:04:50,070  -->  00:04:56,160
And so the slice begins at index 1 not index 0 but index 1.

60

00:04:56,190  -->  00:05:00,450
So we'll have this right here and we want the slice to continue to the end of the list so that means

61

00:05:00,450  -->  00:05:02,510
we'll just leave the second number here.

62

00:05:02,640  -->  00:05:04,350
The second index blank.

63

00:05:04,410  -->  00:05:09,520
So at this place essentially equals is this list value right here.

64

00:05:13,220  -->  00:05:20,040
So that slice is then passed to join which joins all of that into a single string value with a single

65

00:05:20,030  -->  00:05:22,380
space in between them.

66

00:05:22,440  -->  00:05:26,910
So that's the string value we want for the address let's just store that in a variable named address

67

00:05:26,890  -->  00:05:27,650
.

68

00:05:27,880  -->  00:05:32,360
Now Otherwise if there were no command line arguments then we're going to assume the user has it on

69

00:05:32,370  -->  00:05:36,920
the clipboard and that we should get the address from the text on the clipboard.

70

00:05:36,930  -->  00:05:41,330
So remember anything dealing with the clipboard we're going to have to import the paperclip module.

71

00:05:41,330  -->  00:05:45,990
This is a third party module that you have to install separately which we did in a previous lessoned

72

00:05:46,380  -->  00:05:50,900
web browser and Cis are modules that come with Python when you install it.

73

00:05:51,240  -->  00:05:57,280
So here we just have address equals paperclip paste.

74

00:05:57,310  -->  00:06:02,600
So that takes whatever text is on the clipboard and it returns it as a string which we store to address

75

00:06:02,610  -->  00:06:03,530
.

76

00:06:03,650  -->  00:06:10,040
So by the time the program execution gets passed this if else statement no matter what the address variable

77

00:06:10,190  -->  00:06:13,390
has a string of the address we want to open up.

78

00:06:13,400  -->  00:06:17,230
So let's go ahead and launch that web browser.

79

00:06:17,660  -->  00:06:19,850
We'll have to figure out what address to pass it.

80

00:06:19,860  -->  00:06:21,470
So we're going to look at this.

81

00:06:21,500  -->  00:06:26,690
Let me just copy this and paste it into a comment right here.

82

00:06:26,690  -->  00:06:33,680
That is a HUGE your L you know this part makes sense OK Google dot com and then we can see oh here's

83

00:06:33,690  -->  00:06:41,740
the address and then this looks like some latitude and longitude coordinates and then this data part

84

00:06:41,760  -->  00:06:43,350
which is really confusing.

85

00:06:43,700  -->  00:06:50,660
Let's just check to see if this actually works.

86

00:06:50,690  -->  00:06:56,420
We can get rid of it and hopefully the Google web site is smart enough to figure out what we actually

87

00:06:56,430  -->  00:06:56,640
want.

88

00:06:56,630  -->  00:06:57,290
And it does.

89

00:06:57,290  -->  00:07:03,950
It actually does reroute to that same address you know adding all these pluses for spaces we could do

90

00:07:03,950  -->  00:07:05,370
that but let's see if the

91

00:07:08,270  -->  00:07:14,950
Google Maps Web site is smart enough where if we just enter spaces directly if it can find our address

92

00:07:15,020  -->  00:07:16,410
and it does so that's great.

93

00:07:16,400  -->  00:07:27,470
That means we could just paste this address right here so that we have to go to would be this part.

94

00:07:27,480  -->  00:07:32,630
Google com slash maps full by the address we don't even have to do anything special to it we can just

95

00:07:33,380  -->  00:07:37,310
concatenate this string directly to the end of that your l.

96

00:07:37,350  -->  00:07:40,830
So let's go ahead and I'm going to press control-C and copy that.

97

00:07:40,820  -->  00:07:48,780
So the Web site that we want to open up is going to be this Web site and then concatenate the address

98

00:07:48,770  -->  00:07:50,230
string to the end of that.

99

00:07:50,330  -->  00:07:53,340
And then we go ahead and save that.

100

00:07:53,330  -->  00:07:59,210
So this is nice but it's currently not very convenient to call this script because even on Windows when

101

00:07:59,220  -->  00:08:06,860
I press when Dukie are to bring this up in order to run this program we have to first run Python which

102

00:08:06,870  -->  00:08:12,500
is here in program files slash Phyton 3.5.

103

00:08:12,620  -->  00:08:15,710
exe and then I'm going to have to pass it the full path of

104

00:08:18,860  -->  00:08:24,810
the pipe and script itself which is in my Python scripts start there and then I'm going to have to type

105

00:08:24,810  -->  00:08:29,860
in the address but this is way too much typing that's not really that convenient.

106

00:08:29,900  -->  00:08:35,270
What I want is just to be able to say map it and type in the address.

107

00:08:35,410  -->  00:08:36,780
So in a previous lesson

108

00:08:39,500  -->  00:08:42,030
we learned how to create batch files on Windows

109

00:08:45,260  -->  00:08:49,420
which would make it really easy to run a Python script.

110

00:08:49,520  -->  00:08:54,110
And this applies to Windows only but in the course notes there are steps on how to do something like

111

00:08:54,120  -->  00:08:56,420
this on Mac and Linux as well.

112

00:08:56,450  -->  00:09:01,270
So let's create a batch file that will run our map at that pipe program.

113

00:09:01,430  -->  00:09:06,530
So I just Creevey new file and here are just going to run right on X

114

00:09:09,260  -->  00:09:14,680
and then pass it the path of the script.

115

00:09:16,980  -->  00:09:26,310
And then this percent star syntax just pass just forward any command line arguments to the script.

116

00:09:26,340  -->  00:09:31,900
Make sure I said this to all files and I just save this as map it toward that.

117

00:09:31,910  -->  00:09:38,750
So this is a batch file that is easier to call in the batch file will in turn call map it up high.

118

00:09:39,260  -->  00:09:41,030
Never have that.

119

00:09:41,030  -->  00:09:41,900
I'm all set up.

120

00:09:41,900  -->  00:09:44,570
All I have to do is hit windows key or tape map it

121

00:09:47,470  -->  00:09:56,870
and type out my address and I can bring up a map or we go to a Web site that has a whole bunch of business

122

00:09:56,880  -->  00:09:57,850
addresses on it

123

00:10:00,710  -->  00:10:04,960
and a lot of these Web sites will have links to map Web sites right there.

124

00:10:04,970  -->  00:10:10,980
But if they don't it easily just highlight the address press copy or if somebody sent you an address

125

00:10:10,970  -->  00:10:17,900
in an e-mail or something like that just copy it then press Windows key or then just type map it will

126

00:10:17,900  -->  00:10:22,520
instantly bring up a browser window with the address already plugged in.

127

00:10:22,520  -->  00:10:26,570
So will some of the programs you write will perform huge tasks that save you hours.

128

00:10:26,570  -->  00:10:31,340
It can be just as satisfying to use a program that conveniently saves you a few seconds each time you

129

00:10:31,350  -->  00:10:34,910
perform a common tasks such as getting a map of an address.

130

00:10:34,910  -->  00:10:42,140
So to recap Python comes with a web browser module which has a function called Open which you can pass

131

00:10:42,140  -->  00:10:45,700
a string to open up a web browser.

132

00:10:45,950  -->  00:10:52,410
Well we did and this example was create a small program that will just read an address off of the clipboard

133

00:10:52,460  -->  00:10:59,040
or from the command line arguments and then open that to the Google Maps Web site with the address already

134

00:10:59,030  -->  00:11:00,090
plugged in.
