Did you mention that you have an excel file with a list of all sites? Therefore, you can export the excel file as a csv file, which you can then read values ββfrom python code.
Here is more information about this .
Here's how to work directly with excel files
You can do something line by line:
import csv links = [] with open('urls.csv', 'r') as csv_file: csv_reader = csv.reader(csv_file)
links is now a list of all urls. You can then iterate over the list inside a function that retrieves the page and discards the data.
def extract_social_links(links): for link in links: base_url = link br = mechanize.Browser() cj = cookielib.LWPCookieJar() br.set_cookiejar(cj) br.set_handle_robots(False) br.set_handle_equiv(False) br.set_handle_redirect(True) br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] page = br.open(base_url, timeout=10) links = {} for link in br.links(): if link.url.find('facebook')>=0 or link.url.find('twitter')>=0 or link.url.find('linkedin')>=0 or link.url.find('plus.google')>=0: links[link.url] = {'count': 1, 'texts': [link.text]}
As an aside, you should probably separate the if conditions to make them more readable.
Bhargav
source share