【问题标题】:Looping in a url or scrape data from variation in Url在 url 中循环或从 Url 的变化中抓取数据
【发布时间】:2021-09-03 16:51:15
【问题描述】:

我的目标是将加拿大的所有纬度和经度范围自动输入到下面的代码中,并抓取自动出现的位置。我知道加拿大的范围是纬度 42°N 到 83°N,经度 53°W 到 141°W。我知道如何抓取这种类型的数据,但从来不需要在 url 中循环信息。我担心我会以某种方式创建一个循环,除了让我被网站禁止之外什么都不做。所以任何帮助都会很棒!

import requests

url = "https://www.circlek.com/stores_new.php?lat=43.6529&lng=-79.3849&services=&region=global"

payload={}
headers = {
  'Connection': 'keep-alive',
  'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
  'Accept': '*/*',
  'X-Requested-With': 'XMLHttpRequest',
  'sec-ch-ua-mobile': '?0',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
  'Sec-Fetch-Site': 'same-origin',
  'Sec-Fetch-Mode': 'cors',
  'Sec-Fetch-Dest': 'empty',
  'Referer': 'https://www.circlek.com/store-locator?Canada&lat=43.6529&lng=-79.3849',
  'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
  'dnt': '1'
}

response = requests.request("GET", url, headers=headers, data=payload)

print(response.text)

【问题讨论】:

  • 您可以使用简单的 for 循环来为不同的位置创建 url,我认为这种情况不会发生,但即使搜索一些免费的代理服务器或 vpn 添加到请求中,您也准备好了再次。
  • 你有例子吗?就像我输入该代码的地方一样。会不会:对于 url = 然后获取 url 并添加 lat 和 long 的范围?

标签: python loops postman scrape


【解决方案1】:

正如您评论的那样,您可以像这样放置您的代码,如果不共享 lat_lng 的范围,我猜您的不同经纬度存储在这样的列表中

lat_lng = [(lat,long) for lat,long in zip(range(43,83),range(-141,-53))] #store or create range of latitude and longitude 

for latitude,longitude in lat_lng:
  url = f"https://www.circlek.com/stores_new.php?lat={latitude}&lng={longitude}&services=&region=global"
  payload={}
  headers = {
    'Connection': 'keep-alive',
    'sec-ch-ua': '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://www.circlek.com/store-locator?Canada&lat=43.6529&lng=-79.3849',
    'Accept-Language': 'en-GB,en-US;q=0.9,en;q=0.8',
    'dnt': '1'
  }

  response = requests.request("GET", url, headers=headers, data=payload)

  print(response.json())

你也在函数中环绕。

正如您评论的那样,对于否定安排 range 应该是这样的,它正在工作

lat_lng = [(lat,long) for lat,long in zip(range(43,83),range(-141,-53))]

#[(43, -141), (44, -140), (45, -139), (46, -138), (47, -137), (48, -136),.....]

在上面的输出中,请注意,在 zip 中,我们有一对一的关系,就像一个纬度点到一个经度,但如果你想要一对多看 itertools 模块会有所帮助。

为了更准确的使用,我建议您查看np.arange,您也可以将其用于浮动

np.arange(43,83,0.001)
#array([43.   , 43.001, 43.002, ..., 82.997, 82.998, 82.999])

【讨论】:

  • 我想要的范围是:对于 lat:在 43.0000 和 83.0000 之间,对于 lng:-53.0000 和 -141.0000。我不介意加 1,因为重复项很容易在 excel 和解析中取出。我试图将范围输入为:lat_lng = [((范围 (43.0000,83.0000)),(范围(-53.0000,-141.0000)))]。但是,我的远期尝试没有奏效。
  • @John_Muir 我已经编辑了一些建议。它可能会帮助你。
  • 我运行了:lat_lng = [(lat,long) for lat,long in zip(np.arange(43,83,0.001),np.arange(-141,-53,-0.001 ))] 。看起来它运行了,但输出屏幕中没有显示任何内容。只是空白>>>。可能是服务器无法返回数据?还是我输入的方式不对?
  • 它工作正常,请参阅link
  • 是的,改变 1 确实有效,但最终会丢失很多数据。我想使用你使用 np.arange 的方法,所以它增加了 0.001 倍。
猜你喜欢
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-30
相关资源
最近更新 更多