从具有相同网址的两个不同页面抓取数据 [英] Scraping data from two different pages having same url

查看:31
本文介绍了从具有相同网址的两个不同页面抓取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从该站点中删除数据

I am trying to scrap data from this site

http://www.professorpaddle.com/rivers/riverlist.asp

对于不同的州,url是相同的.例如,华盛顿页面和俄勒冈页面具有相同的url.如何编写一个脚本来根据用户在python中的选择为每个州抓取数据?

For different states the url is same.For example washington page and oregon page have same url.How to write a single script to scrap data for each state based on user's choice in python?

推荐答案

在这种情况下,数据是在页面上动态创建的.所以你应该做一些post请求来从服务器获取数据.您可以使用 requests 来做到这一点.如果您使用 Firefox 或 Google Chrome,您可以使用检查工具来查找页面的 javascript 执行的请求类型.在这种特定情况下,您可以通过以下方式获取数据:

In this case, the data is created dynamically on the page. So you should do some post requests to get the data from the server. You can do that you using requests. If you use Firefox or Google Chrome you can use the inspect tool to find the kind of requests the page's javascript do. In this specific case, you can get the data this way:

import requests

# for Washington
data = requests.post("http://www.professorpaddle.com/rivers/riverlist.asp", data={"hstateid":13}).text 

获取所有数据:

all_data = []
for state in range(65): # I got this range manually 
    data = requests.post("http://www.professorpaddle.com/rivers/riverlist.asp", data={"hstateid":state}).text
    all_data.append(data)

这篇关于从具有相同网址的两个不同页面抓取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆