0

I have a problem to cut the url that i get as result from Beautifulsoup, i've used this code to retrieve the url.

import urllib2
from bs4 import BeautifulSoup

url = 'http://192.168.0.184:88/cgi-bin/CGIProxy.fcgi? cmd=snapPicture&usr=USER&pwd=PASS'
html = urllib2.urlopen(url)
soup = BeautifulSoup(html, "html5lib")
imgs = soup.findAll("img")
print imgs
print imgs[1:]

As result from print imgs i get [<img src="../snapPic/Snap_20160401-110642.jpg"/>] I want to cut the unwanted characters from this string so i try to use for eg. print imgs[1:] but as result i get []

Any tips or solutions? I want to rebuild the imgs string to the correct image url imgs string = <img src="../snapPic/Snap_20160401-110642.jpg"/> wanted result = http://192.168.0.184:88/snapPic/Snap_20160401-110642.jpg

  • what do you get with print imgs? – Whitefret Apr 1 '16 at 9:43
1

try this

import urllib2
from bs4 import BeautifulSoup

url = 'http://192.168.0.184:88/cgi-bin/CGIProxy.fcgi? cmd=snapPicture&usr=USER&pwd=PASS'
html = urllib2.urlopen(url)
soup = BeautifulSoup(html, "html5lib")
imgs = soup.findAll("img")
print imgs
for img in imgs:
   print img["src"].replace("..","http://192.168.0.184:88")
| improve this answer | |
  • I get this as result [<img src="../snapPic/Snap_20160401-114529.jpg"/>] Traceback (most recent call last): File "foscam1.py", line 12, in <module> print img[1:] File "/usr/local/lib/python2.7/dist-packages/bs4/element.py", line 958, in getitem return self.attrs[key] TypeError: unhashable type – dverleysen Apr 1 '16 at 9:46
  • I can only see partial stack trace. – WhoAmI Apr 1 '16 at 10:00
  • From your response the error occurs in line five which is html = urllib2.urlopen(url). – WhoAmI Apr 1 '16 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.