User Controls

py fags, why my beautifulsoup throwing an error when trying to get ['src'] of an imge

  1. #1
    see video of what I was trying to do and the error:


    https://youtu.be/tp36CbFga_8?t=5m1s
  2. #2
    starts at 5 minutes
  3. #3
    Lanny Bird of Courage
    That's a really horrific way to present code, why not just copy paste the source and stack trace?

    Anyway, I don't think that's how you use find, it's not a variadic method. You probably want .select() instead, at least from looking at the docs.
  4. #4

    import requests
    from bs4 import BeautifulSoup as bs

    def scrape():
    data = bs(requests.get('http://twitter.com/thefiltration').text, 'html.parser')

    name = data.find('h1', 'ProfileHeaderCard-name').text
    username = data.find('a', 'ProfileHeaderCard-screennameLink').text
    avatar = data.find('img', 'ProfileAvatar-image')['src']
    banner = data.find('img','ProfileCanopy-headerBg', 'div')['src']
    joindata = data.find('span','ProfileHeaderCard-joinDateText').text
    about = data.find('p', 'ProfileHeaderCard-bio u-dir').text

    print banner

    scrape();
  5. #5
    Sophie Pedophile Tech Support
    What does the error message say?
  6. #6
    https://gyazo.com/ea420f84a7e4e771875b34164043360b
  7. #7
    Sophie Pedophile Tech Support
    Originally posted by Iam https://gyazo.com/ea420f84a7e4e771875b34164043360b

    Ah, this means the image doesn't have an attribute called 'src'. So the code is fine, you're just looking for an attribute that isn't there. Does it object if you just remove `['src']`?
  8. #8
    Lanny Bird of Courage
    Originally posted by Iam

    import requests
    from bs4 import BeautifulSoup as bs

    def scrape():
    data = bs(requests.get('http://twitter.com/thefiltration').text, 'html.parser')

    name = data.find('h1', 'ProfileHeaderCard-name').text
    username = data.find('a', 'ProfileHeaderCard-screennameLink').text
    avatar = data.find('img', 'ProfileAvatar-image')['src']
    banner = data.find('img','ProfileCanopy-headerBg', 'div')['src']
    joindata = data.find('span','ProfileHeaderCard-joinDateText').text
    about = data.find('p', 'ProfileHeaderCard-bio u-dir').text

    print banner

    scrape();

    thanks blood.

    What happens if you try

    banner = data.select('.ProfileCanopy-headerBg img')[0]['src']


    ?
    The following users say it would be alright if the author of this post didn't die in a fire!
  9. #9
    Originally posted by Lanny thanks blood.

    What happens if you try

    banner = data.select('.ProfileCanopy-headerBg img')[0]['src']


    ?

    worked nigger, thanks :) I guess you have to do it all in one run like you did it rather than 'div', 'identifier', 'etc'
  10. #10
    cerakote African Astronaut
    if this twitter profile scraper does what i think it does, is there any way i can get a copy of this
Jump to Top