用于python的Web scraping remax.com
这与我的问题相似。回答得很好。现在我有了一些工作,我现在尝试做的是,而不是手动输入url来获取数据。我想开发一个函数,只接收地址和zipcode并返回我想要的数据 现在的问题是修改url以获得正确的url。比如说用于python的Web scraping remax.com,python,web-scraping,beautifulsoup,urllib,Python,Web Scraping,Beautifulsoup,Urllib,这与我的问题相似。回答得很好。现在我有了一些工作,我现在尝试做的是,而不是手动输入url来获取数据。我想开发一个函数,只接收地址和zipcode并返回我想要的数据 现在的问题是修改url以获得正确的url。比如说 url = 'https://www.remax.com/realestatehomesforsale/25-montage-way-laguna-beach-ca-92651-gid100012499996.html' 我发现除了地址、州和zipcode之外,还有一个数字,即gid
url = 'https://www.remax.com/realestatehomesforsale/25-montage-way-laguna-beach-ca-92651-gid100012499996.html'
我发现除了地址、州和zipcode之外,还有一个数字,即gid100012499996,似乎每个地址都是唯一的。所以我不知道如何才能实现我想要的功能
这是我的密码:
import urllib
from bs4 import BeautifulSoup
import pandas as pd
def get_data(url):
hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
'Accept-Encoding': 'none',
'Accept-Language': 'en-US,en;q=0.8',
'Connection': 'keep-alive'}
request = urllib.request.Request(url, headers=hdr)
html = urllib.request.urlopen(request).read()
soup = BeautifulSoup(html,'html.parser')
foot = soup.find('span', class_="listing-detail-sqft-val")
print(foot.text.strip())
url = 'https://www.remax.com/realestatehomesforsale/25-montage-way-laguna-beach-ca-92651-gid100012499996.html'
get_data(url)
我想要的是与上面类似的东西,但是get_data()将接收地址、状态和zipcode。如果这个问题不适合这个网站,我深表歉意。这个网站有一个JSON API,可以让您获得给定矩形中属性的所有详细信息。矩形由西北角和东南角的纬度和经度坐标表示。以下请求显示了可能的搜索:
import requests
params = {
"nwlat" : 41.841966864112, # Calculate from address
"nwlong" : -74.08774571289064, # Calculate from address
"selat" : 41.64189784194883, # Calculate from address
"selong" : -73.61430363525392, # Calculate from address
"Count" : 100,
"pagenumber" : 1,
"SiteID" : "68000000",
"pageCount" : "10",
"tab" : "map",
"sh" : "true",
"forcelatlong" : "true",
"maplistings" : "1",
"maplistcards" : "0",
"sv" : "true",
"sortorder" : "newest",
"view" : "forsale",
}
req_properties = requests.get("https://www.remax.com/api/listings", params=params)
matching_properties_json = req_properties.json()
for p in matching_properties_json[0]:
print(f"{p['Address']:<40} {p.get('BedRooms', 0)} beds | {int(p.get('BathRooms',0))} baths | {p['SqFt']} sqft")
如果您有一个地址,那么您需要计算该地址的纬度和经度。然后在其周围为NW和SE角创建一个小矩形。然后用这些数字建立一个URL。然后,您将获得该区域所有属性的列表(希望为1)
要制作搜索方块,您可以使用以下内容:
lat = 41.841966864112
long = -74.08774571289064
square_size = 0.001
params = {
"nwlat" : lat + square_size,
"nwlong" : long - square_size,
"selat" : lat - square_size,
"selong" : long + square_size,
"Count" : 100,
"pagenumber" : 1,
"SiteID" : "68000000",
"pageCount" : "10",
"tab" : "map",
"sh" : "true",
"forcelatlong" : "true",
"maplistings" : "1",
"maplistcards" : "0",
"sv" : "true",
"sortorder" : "newest",
"view" : "forsale",
}
square\u size
需要根据您的地址的准确性进行调整。所以您正在寻找一种方法来为给定地址生成gid
。@smac89这肯定会有所帮助,是的。@smac89您知道如何做这样的事情吗?我的意思是,您自己不可能获得gid,因为它似乎是为每个列表自动生成的。因此,您需要做的是找到一个api,您可以通过某种方式对其进行操作,以获得您所看到的实际列表。我向您展示了该页面上其中一个表单使用的示例API,因此您可以进一步研究它。这是我能够从其中一个API中提取数据的一个示例:这是生成的curl命令:给出这个问题的实际解决方案需要一些挖掘,我现在没有时间。打开chrome调试器,开始检查页面上的API调用,直到找到正确的API。您也可以将我发布的curl请求分割成可管理的部分,这样您就可以使用它发出多个请求。嗯。这就是我现在所能做的。不幸的是,网络抓取的危害之一是,你必须深入挖掘,找到正确的解决方法。API是一种可能的路径,但是HTML可能有另一个故事要讲,所以如果我有属性列,比如地址、纬度和经度。我如何获得您得到的结果?我假设se位于东南部?我假设soI尝试了您的方法,但遇到了一些错误,您能看到这一点吗?您在哪里找到JSON API的?