O'Reilly Forums: Chapter 2 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Chapter 2 make "coffee" more informative

#1 User is offline   muzima 

  • New Member
  • Pip
  • Group: Members
  • Posts: 8
  • Joined: 21-December 13
  • Gender:Male
  • Location:Moscow,Russia
  • Interests:Snowboarding, classical music, Greek language. I'd like to learn how to create apps for my Android. Aim - creating app for my Greek dictionary by my own.

Posted 21 December 2013 - 12:51 PM

I have added some commands in my code to make the program more informative.


import urllib.request
import time

price = 99.9
while price > 4.4:

time.sleep(2)


page = urllib.request.urlopen("http://beans-r-us.appspot.com/prices-loyalty.html")

text = page.read().decode("utf8")

start = text.find("$")+1

end = start + 4

price = float(text[start:end])

if price > 4.4:

print("Price is too high")

print(price)


print(price)
print("Buy")

This post has been edited by muzima: 21 December 2013 - 12:52 PM

0

#2 User is offline   Biotz 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 07-February 14

Posted 07 February 2014 - 10:21 AM

Hi guyz, i am new in programming... and i started to learn second chapter


import urllib.request

page = urllib.request.urlopen("http://beans-r-us.biz/prices.html")
text = page.read().decode("utf8")

print(text)


for this program i got some error like this


screwedge@ubuntu:~/headfirstpython review$ python3 2.py
Traceback (most recent call last):
File "/usr/lib/python3.3/urllib/request.py", line 1252, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.3/http/client.py", line 1061, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.3/http/client.py", line 1099, in _send_request
self.endheaders(body)
File "/usr/lib/python3.3/http/client.py", line 1057, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.3/http/client.py", line 902, in _send_output
self.send(msg)
File "/usr/lib/python3.3/http/client.py", line 840, in send
self.connect()
File "/usr/lib/python3.3/http/client.py", line 818, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.3/socket.py", line 417, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "2.py", line 3, in <module>
page = urllib.request.urlopen("http://beans-r-us.biz/prices.html")
File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.3/urllib/request.py", line 473, in open
response = self._open(req, data)
File "/usr/lib/python3.3/urllib/request.py", line 491, in _open
'_open', req)
File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
result = func(*args)
File "/usr/lib/python3.3/urllib/request.py", line 1272, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib/python3.3/urllib/request.py", line 1255, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 2] No such file or directory>

can u guys help me to find the solution( i am using ubuntu)
0

#3 User is offline   #TM# 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 44
  • Joined: 10-December 12
  • Gender:Not Telling

Posted 09 February 2014 - 06:10 AM

View PostBiotz, on 07 February 2014 - 10:21 AM, said:

Hi guyz, i am new in programming... and i started to learn second chapter


import urllib.request

page = urllib.request.urlopen("http://beans-r-us.biz/prices.html")
text = page.read().decode("utf8")

print(text)


for this program i got some error like this


screwedge@ubuntu:~/headfirstpython review$ python3 2.py
Traceback (most recent call last):
File "/usr/lib/python3.3/urllib/request.py", line 1252, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.3/http/client.py", line 1061, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.3/http/client.py", line 1099, in _send_request
self.endheaders(body)
File "/usr/lib/python3.3/http/client.py", line 1057, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.3/http/client.py", line 902, in _send_output
self.send(msg)
File "/usr/lib/python3.3/http/client.py", line 840, in send
self.connect()
File "/usr/lib/python3.3/http/client.py", line 818, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.3/socket.py", line 417, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "2.py", line 3, in <module>
page = urllib.request.urlopen("http://beans-r-us.biz/prices.html")
File "/usr/lib/python3.3/urllib/request.py", line 160, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.3/urllib/request.py", line 473, in open
response = self._open(req, data)
File "/usr/lib/python3.3/urllib/request.py", line 491, in _open
'_open', req)
File "/usr/lib/python3.3/urllib/request.py", line 451, in _call_chain
result = func(*args)
File "/usr/lib/python3.3/urllib/request.py", line 1272, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib/python3.3/urllib/request.py", line 1255, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 2] No such file or directory>

can u guys help me to find the solution( i am using ubuntu)


@Biotz

Dear Biotz, the problem with your code probably is the site you are trying to read.

Try the URLs of the below three links instead of "http://beans-r-us.biz/prices.html" in your code.

B)

1. beans r us first link

2. beans r us second link

3. beans r us third link

Hope it helps !
:)

This post has been edited by #TM#: 09 February 2014 - 06:11 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users