我是 Python 新手,正在尝试构建一些小型网络爬虫。我正在尝试使用BeautifulSoup在Python 2.7中编写该程序,该程序将从该页面和后续页面中提取所有配置文件 URL
http://www.bda-findadentist.org.uk/pagination.php?limit=50&page=1
在这里,我试图抓取链接到详细信息页面的所有 URL,例如这个
http://www.bda-findadentist.org.uk/practice_details.php?practice_id=6034&no=61881
但是,我不知道如何让我的程序识别这些 URL。它们不在 DIV 类或 ID 中,而是封装在 TD bgcolor 标记中
<td bgcolor="E7F3F1"><a href="practice_details.php?practice_id=6034&no=61881">View Details</a></td>
请告知我如何让我的程序识别这些 URL 并抓取它们。我尝试了以下方法,但都没有奏效
for link in soup.select('td bgcolor=E7F3F1 a'):
for link in soup.select('td#bgcolor#E7F3F1 a'):
for link in soup.findAll('a[practice_id=*]'):
我的完整程序如下:
import requests
from bs4 import BeautifulSoup
def bda_crawler(pages):
page = 1
while page <= pages:
url = 'http://www.bda-findadentist.org.uk/pagination.php?limit=50&page=' + str(page)
code = requests.get(url)
text = code.text
soup = BeautifulSoup(text)
for link in soup.findAll('a[practice_id=*]'):
href = "http://www.bda-findadentist.org.uk" + link.get('href')
print (href)
page += 1
bda_crawler(2)
请帮忙
非常感谢