用python抓取网站搜索栏



我正试图为一个个人项目编写一些代码,我可以从一个网站上抓取数据,同时也使用该网站的查询框。

此外,我试图使用的网站是https://www.latlong.net/convert-address-to-lat-long.html,我正试图有我的程序的一部分,你输入你的地址。

然后请求转到url的地址搜索栏并执行查询,然后从站点提取lat/lon元素并将其存储在数据框架中。

我知道我将需要使用美丽的汤,从我读到的,可能是机械化和硒,但我有点迷失了试图阅读机械化。

您可能希望使用后端端点。

例如:

import pandas as pd
import requests
from urllib.parse import urlencode
search_query = "Berlin, Germany"
payload = {
"c1": search_query,
"action": "gpcm",
"cp": "",
}
headers = {
"content-type": "application/x-www-form-urlencoded",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36",
"referer": "https://www.latlong.net/convert-address-to-lat-long.html",
"x-requested-with": "XMLHttpRequest",
"cookie": "".join(
f"{k}={v}" for k, v
in requests.get("https://www.latlong.net").cookies.get_dict().items()
),
}
response = requests.post(
"https://www.latlong.net/_spm4.php",
data=urlencode(payload),
headers=headers,
).text
df = pd.DataFrame(
[[*search_query.split(", "), *response.split(",")]],
columns=["City", "Country", "Latitude", "Longitude"],
)
print(df)

输出:

City  Country   Latitude  Longitude
0  Berlin  Germany  52.520008  13.404954

p。不要过度使用,因为他们会限制你的请求。或者使用VPN继续查询。

最新更新