我正在尝试抓取以下网站:https://www.climatempo.com.br/climatologia/558/saopaulo-sp。它有两个下拉菜单,第二个取决于第一个,所以我选择通过scrapy splash使用scrapy和splash。在
我需要通过先选择州,然后选择城市来自动改变位置。但我不想改变名单。我的蜘蛛是(调试打印):
import scrapy
from scrapy_splash import SplashRequest, SplashFormRequest
class ExampleSpider(scrapy.Spider):
name = 'climatologia'
def start_requests(self):
urls = ['https://www.climatempo.com.br/climatologia/558/saopaulo-sp']
for url in urls:
yield SplashRequest(url=url, callback=self.parse,
endpoint='render.html',
args={'wait': 0.5},)
def parse(self, response):
print(response.url)
state = response.css("select.slt-geo")[0].css("option::attr(value)").extract()
print(state)
return SplashFormRequest(response.url, method='POST',
formdata={'sel-state-geo': 'SP'},
callback=self.state_selected,
args={'wait': 0.5})
def state_selected(self, response):
print('\t:+)\t:+)\t:+)\t:+)\t:+)\t:+)')
print(response.css("select.slt-geo")[0].css("option::text").extract())
print(response.css("select.slt-geo")[1].css("option::text").extract())
如果你必须使用网站菜单,我建议Selenium做这个工作。编写Splash脚本的唯一方法是通过LUA脚本。您必须发送到execute端点并创建LUA脚本。我找到了你试图选择的选项,但没有找到提交表格的地点或它在网站上的功能。我确实要翻译成英语。在
我的建议是在浏览器检查器中查找这样的端点,这是几个看起来特别有趣的端点之一: https://www.climatempo.com.br/json/busca-estados
这个端点提供如下json
希望这是另一种方法来获得你正在寻找的数据?在
然后可以使用普通请求来获取数据。你只需要提出同样的请求。通常添加accept、useragent和requested with header就足够了。在
相关问题 更多 >
编程相关推荐