-1

I can’t get python to open a link that uses the contents of a .txt file as a query string. I’m working on Python 3.7.0 and was able to write code that opens the website and checks a string that I’ve input directly, as well as open my text file and print the contents, but when I try to make the text file’s contents a query it throws an error.

I added lines that print the link that I would need to open to make sure it comes out correctly and that works fine, I can copy and paste it into my browser and get a correct result.

Here's the code I used

And a screenshot of the error I get

I'm a total beginner at this so any suggestions or explanations would be lifesavers!

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
senc
  • 3
  • 2
  • 3
    Hello, welcome to StackOverflow! Please do not post code as images. You can ready more about why here: https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question – Matt Messersmith Oct 07 '18 at 19:41
  • For such a small amount of code, you can (and should) include it all in your question. – alexis Oct 07 '18 at 19:44
  • Try to debug the problem by copy-pasting the entire file's contents into your source code, and defining it as a literal string. I suspect that you'll have the same problem, i.e. it's not the source of the text but the length, or something else. (Perhaps you made too many calls and the website is refusing more? Who knows.) Anyway your code looks like you're reading from the file correctly. – alexis Oct 07 '18 at 19:49

2 Answers2

0

The error is with the string being passed to the urlopen(). When it tries to open the link you get an HTTP 400 : Bad request error which means that something is wrong with the link you provided. The text possibly has spaces and you aren't escaping the characters properly. Here is the link which could help you.

Alternatively, you could also use the Python Requests library.

(Please include the code in the question rather than screenshot)

  • @senc Sure no problem. If you do have work with getting data from url, definitely check out the Requests library I mentioned in the answer. – Saket Kumar Singh Oct 09 '18 at 06:37
0

Check out the http you’re requesting does ‘actually’ exists. Moreover, I’m not sure how’s your .txt file looks like, but reexamine the code (.read() part) to make sure the data you wanted to add as a query is being handled correctly.

jihan1008
  • 340
  • 1
  • 10