【发布时间】:2018-01-28 15:18:21
【问题描述】:
我正在尝试收集有关赛狗比赛的信息。比如我想刮http://www.gbgb.org.uk/RaceCard.aspx?dogName=Hardwick%20Serena。此页面显示了狗 Hardwick Serena 的所有结果,但它分为几页。
检查页面,它显示在“下一页”按钮下:
<input type="submit" name="ctl00$ctl00$mainContent$cmscontent$DogRaceCard$lvDogRaceCard$ctl00$ctl03$ctl01$ctl12" value=" " title="Next Page" class="rgPageNext">.
我希望有一个 HTML 链接,可以用于下一次抓取迭代,但没有运气。 进一步检查,通过查看网络流量,显示浏览器为 __VIEWSTATE 等发送了一个非常长的(散列的?)字符串。可能保护数据库?
我正在寻找一种方法来抓取一条狗的所有页面,方法是遍历所有页面,或者增加页面长度以在第 1 页上显示 100 多行。底层数据库是 .aspx。
我正在使用 Python 3.5 和 BeautifulSoup。
当前代码:
import requests
from bs4 import BeautifulSoup
url = 'http://www.gbgb.org.uk/RaceCard.aspx?dogName=Hardwick%20Serena'
with requests.session() as s:
s.headers['user-agent'] = 'Mozilla/5.0'
r = s.get(url)
soup = BeautifulSoup(r.content, 'html5lib')
target = 'ctl00$ctl00$mainContent$cmscontent$DogRaceCard$btnFilter_input'
data = { tag['name']: tag['value']
for tag in soup.select('input[name^=ctl00]') if tag.get('value')
}
state = { tag['name']: tag['value']
for tag in soup.select('input[name^=__]')
}
data.update(state)
numberpages = int(str(soup.find('div', 'rgWrap rgInfoPart')).split(' ')[-2].split('>')[1].split('<')[0])
# for page in range(last_page + 1):
for page in range(numberpages):
data['__EVENTTARGET'] = target.format(page)
#data['__VIEWSTATE'] = target.format(page)
print(10)
r = s.post(url, data=data)
soup = BeautifulSoup(r.content, 'html5lib')
tables = soup.findChildren('table')
my_table = tables[9]
rows = my_table.findChildren(['th', 'tr'])
tabel = [[]]
for i in range(len(rows)):
cells = rows[i].findChildren('td')
tabel.append([])
for j in range(len(cells)):
value = cells[j].string
tabel[i].append(value)
table = []
for i in range(len(tabel)):
if len(tabel[i]) == 16:
del tabel[i][-2:]
table.append(tabel[i])
【问题讨论】:
-
挺有意思的,但是把页面大小设置成最大然后刷表呢?
-
另外看看Robobrowser
标签: python web-scraping beautifulsoup