-1

I am trying to find product names in an xml file i downloaded. I have figured out how to display every result using a while loop. My problem is, i want to only display the first 10 results. Also, i need be able to call each result individually.

For example: print(read_xml_code.start_tag_5) would print the 5th product in the XML file. print(read_xml_code.start_tag_10) would print the 10th

here is my code so far:

# Define the Static webpage XML file
static_webpage_1 = 'StaticStock/acoustic_guitar.html'


def Find_static_webpage_product_name():
    # Open and read the contents of the first XML file
    read_xml_code = open(static_webpage_1, encoding="utf8").read()
    # Find and print the static page title.
    start_tag = '<title><![CDATA['
    end_tag = ']]></title>'
    end_position = 0
    starting_position = read_xml_code.find(start_tag, end_position)
    end_position = read_xml_code.find(end_tag, starting_position)
    while starting_position != -1 and end_position!= -1:
        print(read_xml_code[starting_position + len(start_tag) : end_position]+ '\n')
        starting_position = read_xml_code.find(start_tag, end_position)
        end_position = read_xml_code.find(end_tag, starting_position)

#call function
Find_static_webpage_product_name()
Tomble
  • 53
  • 2
  • 12
  • So add code to a) count the output and stop when you've reached the number you want, and b) skip the first 4 products and then output the next one. – Ken White May 15 '18 at 17:52
  • You could insert a counter in your while loop and a `break` statement, when your counter variable reaches 10. – Merlin1896 May 15 '18 at 17:52

1 Answers1

0

There is an HTML parser in the python standard library (python 3): https://docs.python.org/3/library/html.parser.html

You can easily wait for the tag event and do some counting with a member variable for instance.

Also, do not forget to close your resources (with open(static_webpage_1, encoding="utf8") as f:...)

ant1g
  • 969
  • 9
  • 13