简体   繁体   中英

Can't enter txt file contents as query string using python

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!

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM