You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
structure_trees/a_tree_scraping_from_web.ipynb

15 KiB

a tree extending to the web, pasted with images of the named fruit. the process is done by making a request to the web and scraping an image returned from the request.

modified from the hanging tree program

In [129]:
fruit_list = ["apricot","blood orange","currant","durian","egg fruit","fig","guava",
              "hawthorne","jujube","kiwi","lychee","mandarin","nectarine","olive","persimmon","quandong","rambutan","star fruit",
              "tangor","ugli fruit","vanilla","water chestnut","ximenia","yuzu","zhe"]
# additionally: longan yumberry sugarcane 
In [6]:
# for each word in list, make a request to search that keyword in a search engine
import requests
from bs4 import BeautifulSoup

def getdata(url):
    r = requests.get(url)
    return r.text

htmldata = getdata("https://www.geeksforgeeks.org/")
soup = BeautifulSoup(htmldata, 'html.parser')
for item in soup.find_all('img'):
    print(item['src'])
https://media.geeksforgeeks.org/wp-content/cdn-uploads/write_ndi_20210312.svg
https://media.geeksforgeeks.org/wp-content/cdn-uploads/practice_ndi_20210312.svg
https://media.geeksforgeeks.org/wp-content/cdn-uploads/premium_ndi_20210312.svg
https://media.geeksforgeeks.org/wp-content/cdn-uploads/jobs_ndi_20210312.svg
https://media.geeksforgeeks.org/wp-content/cdn-uploads/20220228124519/Artboard-6-min.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/GunDetectionUsingPythonOpenCV/gundetectionusingpython20220310130800-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/HowtoPerformCRUDOperationsinFlutterApplicationusingMySQLDatabase/HowtoPerformCRUDOperationsinFlutterApplicationusingMySQLDatabase20220304112711.jpg
https://videocdn.geeksforgeeks.org/geeksforgeeks/ImplementingSimpleandCustomAlertDialoginFlutterApplication/AlertDialoginFlutterApplication20220303121322-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/BuildaSimpleStopwatchApplicationinFlutter/BuildaSimpleStopwatchApplicationinFlutter20220302114511-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/MLTreatingCategoricalDataImplementationinPython/TreatingCategoricalDataImplementationinPython20220301102144-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/GettingStartedwithMaterialDesigninFlutter/MaterialDesigninFlutter20220228105528-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/HowtoBuildaToDoApplicationinFlutter/HowtoBuildaToDoApplicationinFlutter20220226163539-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/HowtoUseGoogleSigninWithFirebaseinFlutter/HowtoUseGoogleSigninWithFirebaseinFlutter20220225131450-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/ImplementingSwipetoUnlockFeatureinFlutter/HowtoImplementSwipetoUnlockFeatureinFlutterApplication20220224115856-small.png
https://videocdn.geeksforgeeks.org/geeksforgeeks/MachineLearningHandlingCategoricalData/MachineLearningHandlingCategoricalData20220223123041-small.png
https://media.geeksforgeeks.org/wp-content/post-ads-banner/2021-12-29-11-18-16-DSA_Ad_icon (1).png
https://media.geeksforgeeks.org/wp-content/post-ads-banner/2021-12-29-16-30-50-CIP_Icon.png
https://media.geeksforgeeks.org/wp-content/post-ads-banner/2021-12-29-11-27-51-SD Icon.png

In [15]:
# Import the beautifulsoup
# and request libraries of python.
import requests
import bs4

# Make two strings with default google search URL
# 'https://google.com/search?q=' and
# our customized search keyword.
# Concatenate them
text= "apple"
url = 'https://duckduckgo.com/?q='+ text + '&atb=v315-5&iar=images&iax=images&ia=images' 
print(url)
#url = 'https://duckduckgo.com/?q=apple&atb=v315-5&iar=images&iax=images&ia=images'
# Fetch the URL data using requests.get(url),
# store it in a variable, request_result.
#request_result=requests.get( url )

# Creating soup from the fetched request
#soup = bs4.BeautifulSoup(request_result.text,
    #            "html.parser")
#print(soup)
https://duckduckgo.com/?q=apple&atb=v315-5&iar=images&iax=images&ia=images
In [130]:
# build the fruit motley tree with extra utilities than the letter tree
In [131]:
class letterLeaf:
    def __init__(self,letter,wordFruit):
        self.leftAlphabet = None
        self.rightAlphabet = None
        self.letter = letter
        # try using a list structure to contain the words in this node? 
        self.wordFruit = wordFruit
In [132]:
# printing tree utility 
# this segment is modified from Shubham Singh(SHUBHAMSINGH10)'s contribution 

# spacer
COUNT = [10]

# print a flat lying tree
# speculation this is a recursion that prints the right leaf until there is nothing left
def print2DUtil_flat(root, space) :
    # Base case
    if (root == None) :
        return
    # Increase distance between levels
    space += COUNT[0]
    # Process right leaf/branch/child first
    print2DUtil_flat(root.rightAlphabet, space)
    print()
    for i in range(COUNT[0], space):
        print(end = " ")
    print(root.letter)
    
    for i in range(COUNT[0], space):
        print(end = " ")
    #print(root.letter) 
    print(root.wordFruit)
    # Process left child
    print2DUtil_flat(root.leftAlphabet, space)

    # Wrapper over print2DUtil()
def print2D(root) :
    #Pass initial space count as 0
    print("here is a tree that's laying on the ground: ")
    print2DUtil_flat(root, 0)
In [133]:
#the input was for an interactive version like text input used by wang, save for later 
def grepFirstLetter(word):
    #word = input()
    firstLetter = word[0]
    return firstLetter
    #print("the letter starts with : {}, and will be inserted under the {} leaf".format(firstLetter, firstLetter))
In [134]:
# it will be parsed from the fruit basket
# pick a fruit
# hang onto tree
# parse the string letter by using the grepFirstLetter
def insertLeaf(root,wordFruit):
    #create new leaf 
    letter = grepFirstLetter(wordFruit)
    #print("first letter of {} is : {} ".format(wordFruit, letter))
    #creating a new node containing firstLetter and wordFruit
    newleaf = letterLeaf(letter,wordFruit)
    #print("test print attributes {} {}".format(newleaf.letter, newleaf.wordFruit))
    # python pointer implementation
    # a root pointer 
    x = root
    # pointer y maintains the trailing
    # pointer of x
    # Pointer to start traversing from root
    # and traverses downward path to search
    # where the new node to be inserted
    x = root

    # Pointer y maintains the trailing
    # pointer of x
    y = None

    while (x != None):
        y = x
        if (letter < x.letter):
            x = x.leftAlphabet
        else:
            x = x.rightAlphabet
    
    # If the root is None i.e the tree is
    # empty. The new node is the root node
    if (y == None):
        y = newleaf

    # If the new key is less then the leaf node key
    # Assign the new node to be its left child
    elif (letter < y.letter):
        y.leftAlphabet = newleaf

    # else assign the new node its
    # right child
    else:
        y.rightAlphabet = newleaf

    # Returns the pointer where the
    # new node is inserted
    return y


# A utility function to do inorder
# traversal of BST
In [135]:
# same deal, insert everything in the list until it's empty 
import random

root = None
# pick a random letter in the alphabet
random_fruit = random.choice(fruit_list)
#print(random_letter)
#insert it into the tree, insert the first one 
root = insertLeaf(root, random_fruit)
# remove that letter from list
fruit_list.remove(random_fruit)
#print(fruit_list)
len_list = (len(fruit_list))
#print(len_list)
while len_list > 0:
    random_fruit = random.choice(fruit_list)
    insertLeaf(root,random_fruit)
    fruit_list.remove(random_fruit)
    #print("inserting and removing letter {} ".format(random_letter))
    len_list -= 1
# keep inserting until the list is empty 
# print tree 
print2D(root)
# can try multiple times for different tree configurations
here is a tree that's laying on the ground: 

                    z
                    zhe

          y
          yuzu

                    x
                    ximenia

w
water chestnut

                                        v
                                        vanilla

                              u
                              ugli fruit

                                                  t
                                                  tangor

                                                            s
                                                            star fruit

                                        r
                                        rambutan

                    q
                    quandong

                                        p
                                        persimmon

                              o
                              olive

                                        n
                                        nectarine

          m
          mandarin

                                                  l
                                                  lychee

                                                            k
                                                            kiwi

                                        j
                                        jujube

                              h
                              hawthorne

                                        g
                                        guava

                                                                      f
                                                                      fig

                                                            e
                                                            egg fruit

                                                  d
                                                  durian

                    c
                    currant

                                        b
                                        blood orange

                              a
                              apricot
In [136]:
# fruits in structured presetations:
# https://zhuanlan.zhihu.com/p/113457497
# https://www.wordmom.com/fruits/that-start-with-w

During a potluck dinner in Beijing Adel brought an dish made from pomegrante seeds. It was in December, the crowd was not used to the fruit salad dish. Adel was the only Iranian there. A talented cook as Adel was, the dish was barely touched. Adel, I think you would agree with me that international potlucks are as bad as they can be. Let's hang the fruits high up - trees are good to store and access memories. For the pomegrantes seeds that I've missed that evening.