使用 python 遍历 URL 以抓取相似的表格

Iterating through URLs with python to scrape similar tables

我能够在目标 URL 上抓取 table,但是当我尝试遍历其余页面时,我得到 - 类型错误:并非所有参数在字符串格式化期间都已转换

有 17 页,所以我将 var (n) 设置为最大值。并使用 for 循环到达连续的页面。如果迭代组件被注释掉,代码就可以工作。可以是一个定义循环来提高代码效率吗?

from urllib2 import urlopen
import requests
from bs4 import BeautifulSoup

n = 17
base_url = 'http://www.lowfloat.com/'
for i in range(1, n+1):
    if (i == 1):
        response = urlopen(base_url)
    response = urlopen(base_url + "all/" %i)
html = response
print (html.response)
#html = requests.get(base_url)
soup = BeautifulSoup(html.content, "html.parser")
table = soup.find('table', attrs={'class': 'stocks'})

def target_row(tag):
    is_row = len(tag.findAll('td')) > 5
    row_name = tag.name == 'tr'
    return is_row and row_name
rows = table.findAll(target_row)
rows = rows[1:]

for row in rows:
    cells = row.findAll('td')
    ticker = cells[0].get_text()
    print "ticker " + ticker

您不需要使用 % 来传递变量:

response = urlopen(base_url + "all/" %i)

应该是:

response = urlopen(base_url + "all/" + str(i))

另外我不明白为什么在第一个中使用这个...