【发布时间】:2017-01-12 23:29:25
【问题描述】:
我有一张桌子,我想提取所有链接,浏览链接并抓取 td class=horse 中的项目。
包含所有链接的表格的主页具有以下代码:
<table border="0" cellspacing="0" cellpadding="0" class="full-calendar">
<tr>
<th width="160"> </th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=NSW">NSW</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=VIC">VIC</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=QLD">QLD</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=WA">WA</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=SA">SA</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=TAS">TAS</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=ACT">ACT</a></th>
<th width="105"><a href="/FreeFields/Calendar.aspx?State=NT">NT</a></th>
</tr>
<tr class="rows">
<td>
<p><span>FRIDAY 13 JAN</span></p>
</td>
<td>
<p>
<a href="/FreeFields/Form.aspx?Key=2017Jan13,NSW,Ballina">Ballina</a><br>
<a href="/FreeFields/Form.aspx?Key=2017Jan13,NSW,Gosford">Gosford</a><br>
</p>
</td>
<td>
<p>
<a href="/FreeFields/Form.aspx?Key=2017Jan13,VIC,Ararat">Ararat</a><br>
<a href="/FreeFields/Form.aspx?Key=2017Jan13,VIC,Cranbourne">Cranbourne</a><br>
</p>
</td>
<td>
<p>
<a href="/FreeFields/Form.aspx?Key=2017Jan13,QLD,Doomben">Doomben</a><br>
</p>
</td>
我目前有查找表格和打印链接的代码
from selenium import webdriver
import requests
from bs4 import BeautifulSoup
#path to chromedriver
path_to_chromedriver = '/Users/Kirsty/Downloads/chromedriver'
#ensure browser is set to Chrome
browser = webdriver.Chrome(executable_path= path_to_chromedriver)
#set browser to Racing Australia Home Page
url = 'http://www.racingaustralia.horse/'
r = requests.get(url)
soup=BeautifulSoup(r.content, "html.parser")
#looks up to find the table & prints link for each page
table = soup.find('table',attrs={"class" : "full-calendar"}). find_all('a')
for link in table:
print link.get('href')
想知道是否有人可以帮助我如何获取代码以单击表格中的所有链接并对每个页面执行以下操作
g data = soup.findall("td",{"class":"horse"})
for item in g_data:
print item.text
提前致谢
【问题讨论】:
-
“点击链接”是什么意思?意思是,转到链接的页面,然后刮掉那里的所有链接?
标签: python selenium beautifulsoup python-requests