Threading help!

+1 Mustafa Fakhrualdin · January 16, 2016
Ok i made a webscrapper that takes Australia weather in 8 different state...

but how ever it displays the out come as:

Syndey
Melbourne
Brisbane
Perth
Adelade
Hobart
Candberra
Darwin
23.1°
20.1°
24.3°
35.8°
28.1°
19.3°
21.1°
32.4°

So how can i orginzise it so it displays the state then the tempreture, state-tempreture and so on.


heres my code 

import requests
from bs4 import BeautifulSoup
from threading import Thread


source_code = requests.get("http://www.bom.gov.au/")
plain_text = source_code.text
soup = BeautifulSoup(plain_text, "html.parser")
states = ["Syndey","Melbourne", "Brisbane", "Perth", "Adelade", "Hobart", "Candberra", "Darwin"]


def showStates():
    
    for a in states:
        print(a)

    

def showTemp():
    for a in  soup.findAll('span', {'class': 'val'}) :
        print(a.contents[0])


showStates()
showTemp()

i want it to look like this:

Syndey
23.1?
Melbourne
20.1?
Brisbane
24.3?
Perth
35.8?
Adelade
28.1?
Hobart
19.3?
Candberra
21.1?
Darwin
32.4?


also i want you to explain how it works so i can learn thank you :)

also ignore these question marks- they arre not part of my code 

Post a Reply

Replies

Oldest  Newest  Rating
0 Mustafa Fakhrualdin · January 18, 2016
that printed the whole list 

['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
26.7°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
22.4°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
27.1°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
18.3°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
34.2°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
20.3°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
26.3°
['Syndey', 'Melbourne', 'Brisbane', 'Perth', 'Adelade', 'Hobart', 'Candberra', '
Darwin']
30.4°
0 Sjoerd van den Belt · January 18, 2016
Hi there,

Just create one function in which you process both the states and the temperatures like this:

def showBoth():
i = 0
for a in soup.findAll('span', {'class': 'val'}) :
   print(states)
   print(a.contents[0])
   i += 1



How this works it we take "i" as basically a variable that counts in which possition we are, then we call the state in that possition of the "states" list. Every state has one temperature in the same order so the two should loop in sync.
  • 1

Python

106,976 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator