Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python Selenium中从JavaScript onClick发出自定义GET请求_Python_Selenium_Web Scraping_Python Requests - Fatal编程技术网

如何在Python Selenium中从JavaScript onClick发出自定义GET请求

如何在Python Selenium中从JavaScript onClick发出自定义GET请求,python,selenium,web-scraping,python-requests,Python,Selenium,Web Scraping,Python Requests,进口 import pandas as pd from bs4 import BeautifulSoup from selenium import webdriver from selenium.webdriver.common.keys import Keys import requests from time import sleep 打开页面 driver = webdriver.Chrome() main_url = 'https://www.samsung.com/ph/storel

进口

import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import requests
from time import sleep
打开页面

driver = webdriver.Chrome()
main_url = 'https://www.samsung.com/ph/storelocator/'
driver.get(main_url)
driver.execute_script("window.scrollTo(0, 500)")
sleep(1)
driver.find_element_by_class_name('cm-cookie-geo__close-cta').click()
如果我只是获取红色箭头所示的请求URL,并用所需的参数替换参数(change nradius=7),则返回纯HTML

我怎样才能让它像单击10km按钮(7km除外)一样更新左侧面板上的列表

我曾尝试过像这样使用cookies(没有成功):

编辑1: 我正在尝试获取无法通过get url获得的纬度和经度。可以在主页上找到它

soup = BeautifulSoup(driver.page_source, 'lxml')
latitude = soup.find('ul', {'id':'store-list'}).find_all('li').find('input', {'class':'lat','type':'hidden'})['value']

查看页面,似乎您最好删除距离属性小于或等于7的元素的html。这是因为当返回地图上商店的搜索时,网站似乎只有nradius的特定参数(即仅允许1、2、5和10公里)


它的工作方式是找到您的位置,并找到所有小于10公里的位置(无论您选择了什么距离)。然后根据您选择的距离(nradius提供的)在地图上显示位置。所有的商店查看页面,似乎您最好删除距离属性小于或等于7的元素的html。这是因为当返回地图上商店的搜索时,网站似乎只有nradius的特定参数(即仅允许1、2、5和10公里)


它的工作方式是找到您的位置,并找到所有小于10公里的位置(无论您选择了什么距离)。然后根据您选择的距离(nradius提供的)在地图上显示位置。所有存储您可以使用请求生成一个简单的get请求,然后使用beautiful soup进行解析。编辑中的代码无法工作的原因是通过get请求的html格式不同。以下几点对我有用

代码
导入请求
从bs4导入BeautifulSoup
参数={
“nRadius”:7,
“纬度”:14.601026,
“经度”:120.984192,
“搜索标志”:“搜索”,
“modelCode”:无,
“categorySubTypeCode”:无,
“localSearchCallYn”:“N”,
}
url='1〕https://www.samsung.com/ph/storelocator/_jcr_content/par.cm-g-store-locator-storelist/'
r=requests.get(url,params=params)
soup=BeautifulSoup(r.text'html.parser')
对于汤中的物品。查找所有('li'):
name=item_holder.find('h2',{'class':'store name'})。text
lat=item_holder.find('input',{'class':'lat','type':'hidden'})['value']
long=item_holder.find('input',{'class':'long','type':'hidden'})['value']
打印('\n'+名称)
打印(横向、纵向)
输出(部分)
您可以使用请求生成一个简单的get请求,然后使用BeautifulSoup进行解析。编辑中的代码无法工作的原因是通过get请求的html格式不同。以下几点对我有用

代码
导入请求
从bs4导入BeautifulSoup
参数={
“nRadius”:7,
“纬度”:14.601026,
“经度”:120.984192,
“搜索标志”:“搜索”,
“modelCode”:无,
“categorySubTypeCode”:无,
“localSearchCallYn”:“N”,
}
url='1〕https://www.samsung.com/ph/storelocator/_jcr_content/par.cm-g-store-locator-storelist/'
r=requests.get(url,params=params)
soup=BeautifulSoup(r.text'html.parser')
对于汤中的物品。查找所有('li'):
name=item_holder.find('h2',{'class':'store name'})。text
lat=item_holder.find('input',{'class':'lat','type':'hidden'})['value']
long=item_holder.find('input',{'class':'long','type':'hidden'})['value']
打印('\n'+名称)
打印(横向、纵向)
输出(部分)
你到底想完成什么?如果我得到半径改变的url,它会返回一个以该经度和纬度为半径的商店列表。您可以从html中删除商店地址/电话号码等。你想从这个页面得到什么?@jarcobi889是的,这是一个公平的点,但我正在尝试通过get url获取无法获得的纬度和经度。但是可以在主页上找到它。我已经编辑了我的帖子来反映这一点。如果您所追求的是long/lat,并且您只想发出直接请求,而不仅仅是使用selenium,那么您需要深入页面上的javascript并找到当您单击10km按钮时触发的事件。这将向您显示当对您看到的url的ajax请求返回时,发送了哪些其他请求,以及页面上还更新了哪些内容。您到底想实现什么?如果我得到半径改变的url,它会返回一个以该经度和纬度为半径的商店列表。您可以从html中删除商店地址/电话号码等。你想从这个页面得到什么?@jarcobi889是的,这是一个公平的点,但我正在尝试通过get url获取无法获得的纬度和经度。但是可以在主页上找到它。我已经编辑了我的帖子来反映这一点。如果您所追求的是long/lat,并且您只想发出直接请求,而不仅仅是使用selenium,那么您需要深入页面上的javascript并找到当您单击10km按钮时触发的事件。这将向您显示当对您看到的url的ajax请求返回时,页面上正在发送哪些其他请求以及正在更新哪些内容。谢谢!但我需要的距离能够超过10公里。我用公认的答案设法解决了这个问题。原来我用错了参数。谢谢!但我需要的距离能够超过10公里。我用公认的答案设法解决了这个问题。结果发现我错误地使用了参数。啊,看来我错误地使用了
params
参数。谢谢啊,看来我用的是
par
soup = BeautifulSoup(driver.page_source, 'lxml')
latitude = soup.find('ul', {'id':'store-list'}).find_all('li').find('input', {'class':'lat','type':'hidden'})['value']
WESTERN APPLIANCE - RECTO
14.604366 120.97991

ANSONS - BINONDO
14.6015268 120.97605479999993

SM APPLIANCE CENTER INC. - LUCKY CHINA TOWN
14.6031205 120.9741785

SM APPLIANCE CENTER INC. - MANILA
14.5904064 120.9830574