Background:
I am in the process of building a program that scrapes weather data from the internet, and displays it to the user as part of a GUI. The user will enter in their location details, specifically their PostCode or ZipNumber, City or Town, Latitude, and Longitude. The program will store these four pieces of information into a textfile, this is so that the details can be read each time the user wants to request weather data, instead of having to enter these details in on each request. The modules that this problem concerns are urllib, and BeautifulSoup.
import urllib.request
from bs4 import BeautifulSoup
The Problem:
I have successfully managed to store the user details into a text file, and also read from it. The code for inserting the data looks like this:
userPostcode = postcodeEntry.get()
userCity = cityEntry.get()
userLat = latitudeEntry.get()
userLong = longitudeEntry.get()
file = open("LocationInfo.txt", 'w')
file.write(str(userPostcode) + "\n")
file.write(str(userCity) + "\n")
file.write(str(userLat) + "\n")
file.write(str(userLong)+ "\n")
file.close()
The structure of the data inside the textfile looks like this:
SK15 IJF
SOME TOWN
54.25
-122.312
The code for reading from the textfile looks like this:
f=open('LocationInfo.txt')
line=f.readlines()
Post = line[0]
Town = line[1]
Lat = line[2]
Long = line[3]
f.close()
The way I have inserted the values of these variables into the URL is by using this method:
page_url = "https://www.metcheck.com/WEATHER/now_and_next.asp?
zipcode=%s+%s&lat=%s&lon=%s" % (Post, Town, Lat, Long)
soup = BeautifulSoup(urllib.request.urlopen(page_url), "lxml")
*note the url is all on one line in the actual program.
The Error:
The error I am receiving is:
Exception in Tkinter callback
Traceback (most recent call last):
Python\Python36-32\lib\http\client.py", line 279, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: <html>
This error only occurs whenever I try and use the variable names assigned to the data in the text file, and try to insert them into the URL using the % method. When I enter the values directly into the URL string, instead of using the variable names, the expected result occurs. Therefore, I have reason to believe that the problem is to do with the variables themselves, and the values, not the actual data which is valid.