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 打开一个页面,有时会有更多页面,在这种情况下,下一页的模式将是 &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