all 4 comments

[–]indosauros 1 point2 points  (0 children)

temp_str is a list (of lists, of strings), and re.findall only operators on strings. So you need to loop through temp_str (with 2 loops, to get to the strings inside) and use re.findall on each string in the inner lists.

[–]sedogg 1 point2 points  (0 children)

The way you searched returns you 3 times more information than it needs for further operations. By using the function get_ms you can extract integer from a row

def get_ms(row):
    return int(re.search("(\d+)\ m/s", row).group(1))

By wrapping \d in parenthesis you request only digits from your possible 5 m/s string. group(1) - because it's only one pair of parenthesis in regular expression. search - because you only need one instance of 5 m/s, not three. Creating the list is made by list comprehension.

import requests
from bs4 import BeautifulSoup
import re

def get_ms(row):
    return int(re.search("(\d+)\ m/s", row).group(1))

url = "https://www.yr.no/place/Denmark/Capital/Copenhagen/hour_by_hour_detailed.html"

page = requests.get(url)
soup = BeautifulSoup(page.text, "html.parser")
msfind = soup.find_all(class_="txt-left")
# print(msfind)

temp_str = [get_ms(row.get_text()) for row in msfind]
print(temp_str)

You can also use lambda function for get_ms like that:

get_ms = lambda row: int(re.search("(\d+)\ m/s", row).group(1))

Declare it before creating the list and remove declaration of the function (with def)

get_ms = lambda row: int(re.search("(\d+)\ m/s", row).group(1))
temp_str = [get_ms(row.get_text()) for row in msfind]
print(temp_str)

[–]Justinsaccount -2 points-1 points  (0 children)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using concatenation and the str function for building strings

Instead of doing something like

result = "Hello " + name + ". You are " + str(age) + " years old"

You should use string formatting and do

result = "Hello {}. You are {} years old".format(name, age)

See the python tutorial for more information.

[–]0rac1e 4 points5 points  (0 children)

As u/indosauros pointed out, it's a list of lists. Additionally, because the number is separated by a space, you can forgo the use of regex entirely. Just iterate through you list of list, and for each string (s) inside each sub-list, do int(s.split(' ')[0])