• n8henrie142

    I have a script that is running perfectly on my Mac, but giving me an error in Pythonista.

    BeautifulSoup is throwing an AttributeError: 'NoneType' object has no attribute 'next_element' on finding all data points in an HTML table: soup.find('table').find_all('td').

    I can verify that soup appears correct and has the td that I'm looking for. I can print soup.find('table') in the console and it is correct. I can break it down to table = soup.find('table'); table.find_all('td'); and it still doesn't work. I've tried changing to the old .findAll instead of .find_all and that doesn't work either.

    In fact, even soup.find('table').find('td') works correctly, but gives the error when changing .find('td') to .find_all('td').

    find_all seems to work in some contexts, e.g. `bs4.BeautifulSoup(requests.get('http://omz-software.com').content).find('p').find_all('a') seems to work fine.

    I can verify the identical code (synced by Dropbox) works fine on Python 2.7.8 in OS X.

    Has anyone run into this?

    posted in Pythonista read more
  • n8henrie142

    Getting all td from the table at w3schools.com/html/html_tables.asp works fine.

    Here's my traceback.

    2014-10-27 13:12:43 /var/mobile/Containers/Data/Application/3664C317-2455-4F95-AFC5-EAF05BC6B8BF/Documents/scratchpad.py :: __main__ ERROR    There was an error.
    2014-10-27 13:12:44 /var/mobile/Containers/Data/Application/3664C317-2455-4F95-AFC5-EAF05BC6B8BF/Documents/scratchpad.py :: __main__ ERROR    'NoneType' object has no attribute 'next_element'
    Traceback (most recent call last):
      File "/var/mobile/Containers/Data/Application/3664C317-2455-4F95-AFC5-EAF05BC6B8BF/Documents/scratchpad.py", line 28, in <module>
        print(len(table.find('td').find_all('td')))
      File "/private/var/mobile/Containers/Bundle/Application/B8E731EE-F2DC-466D-BEEA-D2EF5E76AEAC/Pythonista.app/pylib/site-packages/bs4/element.py", line 1180, in find_all
        return self._find_all(name, attrs, text, limit, generator, **kwargs)
      File "/private/var/mobile/Containers/Bundle/Application/B8E731EE-F2DC-466D-BEEA-D2EF5E76AEAC/Pythonista.app/pylib/site-packages/bs4/element.py", line 497, in _find_all
        return ResultSet(strainer, result)
      File "/private/var/mobile/Containers/Bundle/Application/B8E731EE-F2DC-466D-BEEA-D2EF5E76AEAC/Pythonista.app/pylib/site-packages/bs4/element.py", line 1610, in __init__
        super(ResultSet, self).__init__(result)
      File "/private/var/mobile/Containers/Bundle/Application/B8E731EE-F2DC-466D-BEEA-D2EF5E76AEAC/Pythonista.app/pylib/site-packages/bs4/element.py", line 494, in <genexpr>
        result = (element for element in generator
      File "/private/var/mobile/Containers/Bundle/Application/B8E731EE-F2DC-466D-BEEA-D2EF5E76AEAC/Pythonista.app/pylib/site-packages/bs4/element.py", line 1198, in descendants
        current = current.next_element
    AttributeError: 'NoneType' object has no attribute 'next_element'

    posted in Pythonista read more
  • n8henrie142

    Unfortunately, I would have done that already except that it's a password-protected site I use for work. I haven't been able to replicate yet on a couple of other sites, but I'll try to find a public site that has the same bug.

    posted in Pythonista read more
  • n8henrie142

    As suspected, I wrote html from Pythonista to a pickle file, loaded it on OS X, converted to soup, and had no problem using find_all('td') on OS X.

    I also used difflib to inspect the differences between HTML content of the Pythonista file and that downloaded on OS X , and as far as I can tell the only differences are timestamps (as the content was downloaded minutes apart).

    posted in Pythonista read more
  • n8henrie142

    I understand what he means, and I'll check, but I don't think that would explain in any way why .find('td') would have a result but .find_all('td') would cause an error. It wouldn't even make sense if it came up empty (it should at least find the result that .find() found), but it should definitely not cause an error.

    posted in Pythonista read more
  • n8henrie142

    Thanks for the response.

    Same version on both.

    $ python -c 'import bs4; print(bs4.__version__)'
    4.3.2
    

    I think the thing that seals it as a bug is that soup.find('table').find('td') works, but soup.find('table').find_all('td') throws an error, on the same soup object.

    posted in Pythonista read more
  • n8henrie142

    My question: What is the best way for Python to detect if it is running in Pythonista vs my computer?

    I love using the dropbox module to sync my Pythonista code between devices, and it's nice to edit things from my computer every once in a while.

    Many of my scripts serve the same basic function, but I've had to write a Pythonista version and a separate OSX version of the script, sometimes for minor differences such as getting credentials with keychain vs python-keyring.

    Because Pythonista seems to ignore my shebang, it seems like it would be pretty easy to get a single script running on both platforms without much effort, at least in some cases.

    So far, my best answer is something like:
    <html><pre>
    import sys
    if sys.platform == 'unknown':
    # execute pythonista specific code here, e.g:
    # import keychain
    # mypass = keychain.get_password()
    pass
    elif sys.platform == 'darwin':
    # execute osx specific code here, e.g:
    # import keyring
    # mypass = keyring.backends.OS_X.Keyring.get_password()
    pass
    elif sys.platform == 'linux2':
    # etc
    pass
    </pre></html>
    The only think I don't care for is that sys.platform is 'unknown' -- which might be true for other platforms I haven't explored yet (Android or something).

    Other options:

    • import platform; platform.system(); # returns 'Darwin' on both iOS and OSX, won't work
    • import os; os.name; # returns posix on both, won't work

    Another that might work:
    import os
    os.uname()

    Returns some device-specific information such as the local hostname, which wouldn't be terribly helpful (would work on one iOS device but not the others), but also some kernel information that could be used (but might change as the OS gets updated). E.g. if os.uname()[2] == '12.4.0'

    So far, I think the sys.platform == 'unknown' is the best choice I have, but I wanted to see if anyone had other thoughts or options. Thanks for any help, ahead of time.

    posted in Pythonista read more
  • n8henrie142

    Good thoughts, thanks to both.

    posted in Pythonista read more
  • n8henrie142

    Man, I really wish we had gfm style codeblocks... indentation not showing up above in the code.
    EDIT: Fixed with html and pre. Thanks to ccc. Still wish for the gfm :)

    posted in Pythonista read more
  • n8henrie142

    Howdy,

    I'm writing a little script that uploads the most recent photo to a cloud service, grabs, the direct URL, then throws that through bitly. I can't seem to figure out how to get the filename, so I'm setting it manually via console.alert, and I'm a little confused on what the file extension should be.

    For example, in photos.get_metadata(-1) from a screenshot, I see a dictionary key {PNG} (not sure if backticks make code blocks on this forum...), which is correct, so I set the file extension to png. However, for photos taken with the camera, photos.get_metadata(-1) returns a few keys, including exif data, and a {TIFF} key.

    However, I'm pretty sure that those are .jpg and not .tiff files... so is there any way I can detect what the proper file extension is for each file? I tried looking into the mimetypes module, but it looks like you need a file read to use that, and unfortunately I'm achieving that with photos.save to a StringIO... and I think the photos.save requires an extension (http://omz-software.com/pythonista/docs/ios/Image.html).

    Also, I don't see any pattern that resembles the filenames that photos have when imported through iPhoto or what have you (IMG_XXXX.jpg). Is there any way to find what a photo's filename is?

    Something that could retrieve the path to the file would accomplish both of these, but I'd be surprised if Apple hadn't sandboxed that ability.

    Thanks for any help!

    posted in Pythonista read more

Internal error.

Oops! Looks like something went wrong!