【发布时间】:2015-09-08 08:47:25
【问题描述】:
这是我第一次尝试python并在这里提出问题,所以我提前为这个问题的任何不正确结构道歉。
以下代码运行良好。
我遇到的问题是下面的脚本会刮掉第一页,但是我无法解决如何让代码触发下一个按钮,以便我可以遍历该数据等等。
在浏览器中,当您单击“下一步”按钮时,url 保持不变,但表中的数据已刷新并显示出相应的结果。
所以我假设该站点正在使用 ajax 来重新填充表格。
因此我的问题是如何以编程方式让 python 设置 ajax 以用下一批数据重新填充表?
import requests
from bs4 import BeautifulSoup
from datetime import datetime
# this crawls the url below for Primary and Secondary Schools throughout Australia
# and returns the Index(as a href link) for the details from the Href Link in the Index Output
# get the current date Time for the file labels
f_date = format(datetime.now().strftime('%Y%m%d'))
# set the Index File Name
index_file_title = ('education_primary_secondary_index_' + f_date + '.csv')
index_error_file_title = ('education_primary_secondary_index_errors_' + f_date + '.csv')
f_index = open(index_file_title, 'w')
f_index_errors = open(index_error_file_title, 'w')
def education_primary_secondary_index_spider(max_pages):
page = 1
url = "http://www.australianschoolsdirectory.com.au/search-result.php"
striped_url = "http://www.australianschoolsdirectory.com.au"
while page <= max_pages:
source_code = requests.get(url)
#plain_text = source_code.text
soup = BeautifulSoup(source_code.text, "html.parser")
for school_index_links in soup.find_all('a', {'class': 'clearfix'}):
try:
school_index_href = striped_url + school_index_links.get('href')
f_index.write(str(school_index_href) + '\n')
except Exception as e:
f_index_errors.write(str(page) + '-----' + str(e) + '\n')
pass
# call to Details Module
#education_primary_secondary_details_spider(index_href)
page += 1
education_primary_secondary_index_spider(1)
【问题讨论】:
标签: ajax python-3.x beautifulsoup