from twill.commands import *
from bs4 import BeautifulSoup
from urllib import urlopen
import urllib2
with open('urls.txt') as inf:
urls = (line.strip() for line in inf)
for url in urls:
try:
urllib2.urlopen(url)
except urllib2.HTTPError, e:
print e
site = urlopen(url)
soup = BeautifulSoup(site)
for td in soup.find_all('td', {'class': 'subjectCell'}):
print td.find('a').text
私のコードは、ファイルの各 URL から 1 つのページのみを開きます。さらに多くのページがある場合もあります。その場合、次のページのパターンは &page=x になります。
ここに私が話しているページがあります:
http://www.last.fm/user/TheBladeRunner_/library/tags?tag=long+track http://www.last.fm/user/TheBladeRunner_/library/tags?tag=long+track&page=7