pip install lxml<\/code><\/p>\nStep 5: import the lxml-parser<\/h3>\n
Import the lxml-parser into your Python script as follows:<\/p>\n
import lxml<\/code><\/p>\nStep 6: create a Soup object<\/h3>\n
To run BeautifulSoup, use the command below.<\/span><\/span><\/p>\n#create soup<\/code><\/code>
\nsoup = BeautifulSoup(response.text, 'lxml')
\n<\/code><\/p>\nIn the code above, the first part “response.tex<\/em>t” is the HTML text on which the object “soup<\/em>” is based.<\/span> After all, the “response.text<\/em>” was the result of the requested HTML page as we specified earlier.<\/span> The second part “lxml<\/em>” specifies the parser BeautifulSoup should use to create the object “soup<\/em>“.<\/span><\/span><\/p>\nStep 7: print the Soup object<\/h3>\n
To test if everything works fine you can now run your script.<\/span> Our script currently looks like this.<\/span><\/span><\/p>\n<\/p>\n
\n
And gives us the following result:<\/p>\n<\/h3>\n\n
So this script works.<\/span> And if you look closely you will see that the result looks slightly different from the previous result.<\/span> For example, you will now also see the sections “<html> <body><\/em>” and “<\/body> <\/html><\/em>“.<\/span> This makes sense, because you have requested the HTML content of a web page.<\/p>\nStep 8: use “prettyprint” for nicer results<\/h3>\n\n
To make your results more beautiful (in a “nested structure<\/em>“), you can use the command below<\/p>\nprint(soup.prettify())<\/code><\/p>\n\n
This gives you the following result in which the HTML structure is nested.<\/span> That is to say, for example, that “<rsp stat =” ok “><\/em>” up to “<\/rsp><\/em>” fall under the “<body><\/em>” of the HTML page.<\/p>\n<\/h3>\nStep 9: filtering the print results<\/h3>\n
With the code above you are not there yet, after all you just want to print out the user ID and username of your targeted user.<\/span> For this you need to look in the HTML<\/strong> where exactly the information you are looking for is stored.<\/span> For example, if we start with the username, the username is immediately shown between “<username><\/em>” and “<\/username><\/em>“.<\/span> You can then point to exactly this part in your script using the command below.<\/span><\/span><\/p>\n#filter results<\/code>
\nuser_name = soup.username.text <\/code><\/p>\n\n
The above command searches for the text part which is located in the following structure: html> body> username.<\/span> By using “soup.username.text<\/em>” you directly call the value of the object “username<\/em>“, which in this case is the username.<\/span> If you want to print the variable “user_name<\/em>“, you can do this as follows:<\/p>\nprint(user_name)<\/code><\/p>\nThe result you will see is only the username:<\/span><\/span><\/p>\n<\/p>\n
Step 9: add text to your results<\/h3>\n\n
Your script now does exactly what it should do: it shows an user’s username based on an email address that you have provided.<\/span> You can use the command below to clarify the result you will see.<\/span> So you can enter text yourself.<\/p>\nprint('\\nUsername:',user_name) <\/code><\/p>\nThe result will look like the following:<\/p>\n
<\/p>\n
Step 10: adding multiple filters and printing the results<\/h3>\n\n
With the code above you have only printed the user name, but you also want to know the user ID.<\/span> You can use the code below.<\/p>\n#filter results<\/code>
\nuser_name = soup.username.text<\/code>
\nuser_id = soup.user<\/code>
\nuser_profiel = ('https:\/\/www.flickr.com\/people\/'+user_id.get('id'))<\/code><\/p>\n#print results<\/code>
\nprint('\\nUsername', user_name)<\/code>
\nprint('User ID:', user_id.get('id'))<\/code>
\nprint('Flickr-profile:', user_profiel)<\/code><\/p>\n\n
If you now print everything, you will get the result below.<\/p>\n<\/p>\n
Making your Python script user-friendly<\/h2>\n\n
At this moment your Python script works, but is not very user-friendly yet.<\/span> You always have to change the code of your script to make it work, and you have to do that for every email address you want to check.<\/span> So it’s time to make your script more user-friendly.<\/p>\nStep 1: create your own fancy template<\/h3>\n\n
Many scripts have a nice logo or text in the beginning.<\/span> A script looks much more exciting with some fancy colors or words.<\/span> You can ann some text very simply as follows.<\/p>\nprint()<\/code>
\nprint('\\n*************************************************************************************'<\/code>
\n'\\nPurpose: \\t\\tFind Flickr profiles by email'<\/code>
\n'\\nCopyright: \\tYour Name here')<\/code>
\nprint('*************************************************************************************')<\/code><\/p>\n\n
If you print the script now, you will get the following result.<\/span> Isn’t that cool?<\/p>\n<\/p>\n
Step 2: create new input variables<\/h3>\n\n
The script you have created is currently quite static<\/strong>.<\/span> The script contains the API key of Flickr and the email address you want to investigate.<\/span> If you want to use the script more often, you may want to use the API key and email address as an input value<\/strong> to store in a new variable.<\/span><\/span><\/div>\n<\/div>\n<\/div>\n
\n
For the email address you can do that as follows, let’s just leave the API key for now:<\/p>\nprint('Fill in target email:\\n')<\/code>
\nemailadres = input(\"Email addddres: \")<\/code><\/p>\nPlease note that you will also need to edit the URL in your code.<\/span> After all, you want to use the email address you enter via the input field in the URL you are going to visit.<\/span> This can be done as follows.<\/span><\/span><\/p>\nurl = 'https:\/\/www.flickr.com\/services\/rest\/?method=flickr.people.findByEmail&api_key=3eeb03cea945a8f597c529ebd454051b&find_email='+emailadres+'&format=rest'<\/code><\/p>\n\n
If you now print the result, you will be asked to enter an email address of your target.<\/span> You can do this manually, after which the script will continue to run as before.<\/p>\n<\/p>\n
Save the results to a CSV file<\/h2>\n