Question

I am trying to find a link on a website and open the link in a new tab. If I hide my code in comments and just tell it to open the link in a new tab in chrome it works but add the if statement back and it acts like it isn't finding what I know is in the source code. Any ideas?

import os
import sys
import webbrowser
from bs4 import BeautifulSoup
import urllib2
url='https://twitter.com/nikestore'
page=urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

for tag in soup.find_all('a'):
    tag.get('href')
    if tag.has_attr('http://t.co/'):
       link=tag
       webbrowser.open_new_tab(link)
Was it helpful?

Solution

You are passing the whole Element (<a href="...">) around here. You forgot to assign the tag.get('href') result:

for tag in soup.find_all('a'):
    link = tag.get('href')
    if 'http://t.co/' in link:
       webbrowser.open_new_tab(link)

You can simplify your loop a little by using a regular expression match:

import re

for tag in soup.find_all('a', href=re.compile('^http://t\.co/')):
    link = tag['href']
    webbrowser.open_new_tab(link)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top