23 March, 2010

Grab All Links Of Web Page Using Python

I always try to make useful program to learn a Programming Language. Now I am trying to learn Python. I will try to post my practice code here. Today I will show you how to grab all links of web page using Python's "BeautifulSoup" module.

  1. import urllib,BeautifulSoup
  2. def get_links(url):
  3.      page = urllib.urlopen(url)
  4.      soup = BeautifulSoup.BeautifulSoup(page.read())
  5.      aTag = soup.findAll("a")
  6.      link = []
  7.      for a in aTag:
  8.           if a.has_key("href"):
  9.                link.append(a['href'])
  10.      return link
  11. print get_links("http://localhost/PRACTIECE/a2/")
Enjoy!!!

23 February, 2010

Replace Links With Html

You can replace links with html tag using PHP regular expression.
Here is an example of it.
  1. function replace_link_with_link($string)
  2. {
  3.     $pattern = "|http://(\w+)([A-Z0-9-_:./?]+)*|i";
  4.     $string = preg_replace($pattern,'<a href="\0">\0</a>',$string);
  5.     return $string;
  6. }
Enjoy. Help Helpless.

Regular Expression To Replace All Mentioned Name With Link

In twitter Rana vai gave me a code to replace all mentioned name with link like twitter. Check that.
There was 2 problem.
  1. I anyone use '.' after mentioned name then there will be '.' in link.
  2. There will be '@' in link.
Here is a function. I hope it will work perfectly

  1. function link_from_at($string)
  2. {
  3.     $status = preg_replace("|@([a-zA-Z0-9]+)*|",'@<a href="\1">\1</a>',$string);
  4.     return $status;
  5. }

22 February, 2010

Regular Expression to Find All Mentioned Names by ‘@’

I have seen a cool example about it at Junal vai's blog. Here is a shortcut example of it..
  1. function name_from_at($string)
  2. {
  3.     $pattern = "/@([a-zA-Z0-9]+)/i";
  4.     preg_match_all($pattern,$string,$array);
  5.     return $array[0];
  6. }
Enjoy..... Help the people of Haiti......