【发布时间】:2015-09-18 07:00:30
【问题描述】:
我正在尝试使用 BeautifulSoup 在 Python 2.7 中构建一个程序,该程序将从该页面和后续页面中提取所有配置文件 URL
http://www.reaa.govt.nz/Pages/PublicRegisterSearch.aspx?pageNo=1&name=a*&orgName=&location=&licenceNo=&itemsPerPage=100&sortExpression=2
我已经和这个程序斗争了很长时间,但它仍然不起作用。我想我在 CSS 选择器上搞砸了,但我不确定还能尝试什么。
请指教...我是编程和 python 新手
import requests
from bs4 import BeautifulSoup
def re_crawler(pages):
page = 1
while page <= pages:
url = 'http://www.reaa.govt.nz/Pages/PublicRegisterSearch.aspx?pageNo=' + str(page) + '&name=a*&orgName=&location=&licenceNo=&itemsPerPage=100&sortExpression=2'
code = requests.get(url)
text = code.text
soup = BeautifulSoup(text)
for link in soup.select('tr.alternate td a[id*=ct100_]'):
href = link.get('href')
print (href)
page += 1
re_crawler(2)
【问题讨论】:
-
我在 html 中看到 id 以 ctl00_ 开头,但在您的代码中是 ct100_,可能是拼写错误?
-
非常感谢您注意到这一点,Birei。我没有注意到它是 ctl00_ 并且它也不起作用。感谢您的帮助:)
标签: python-2.7 css-selectors web-scraping beautifulsoup