【发布时间】:2021-06-24 19:28:17
【问题描述】:
我正在尝试抓取来自this link 的高分辨率图像的链接,但只有单击页面,即点击后“点击这里放大图片”(在页面上,它是土耳其语)。
然后我可以使用 Chrome 的“开发者工具”检查它并获取 xpath/css 选择器。到目前为止一切都很好。
但是,您知道在 JS 页面中,您只是无法键入 response.xpath("//blah/blah/@src") 并获取一些数据。我安装 Splash(使用 Docker pull)并配置我的 Scrapy setting.py 文件等以使其工作(这个 YouTube link 帮助。除非你想学习如何做,否则无需访问链接)。 ...并且它适用于其他 JS 网页!
只是...我无法通过这个“单击此处放大图片!” 的事情并得到响应。它给了我null 的响应。
这是我的代码:
import scrapy
#import json
from scrapy_splash import SplashRequest
class TryMe(scrapy.Spider):
name = 'try_me'
allowed_domains = ['arabam.com']
def start_requests(self):
start_urls = ["https://www.arabam.com/ilan/sahibinden-satilik-hyundai-accent/bayramda-arabasiz-kalmaa/17753653",
]
for url in start_urls:
yield scrapy.Request(url=url,
callback=self.parse,
meta={'splash': {'endpoint': 'render.html', 'args': {'wait': 0.5}}})
# yield SplashRequest(url=url, callback=self.parse) # this works too
def parse(self, response):
## I can get this one's link successfully since it's not between js codes:
#IMG_LINKS = response.xpath('//*[@id="js-hook-for-ing-credit"]/div/div/a/img/@src').get()
## but this one just doesn't work:
IMG_LINKS = response.xpath("/html/body/div[7]/div/div[1]/div[1]/div/img/@src").get()
print(IMG_LINKS) # prints null :(
yield {"img_links":IMG_LINKS} # gives the items: img_links:null
我正在使用的 Shell 命令:scrapy crawl try_me -O random_filename.jl
我要抓取的链接的 Xpath:/html/body/div[7]/div/div[1]/div[1]/div/img
当我点击放大时,我实际上可以在我的开发者工具窗口的网络标签上看到我想要的链接,但我不知道如何从该标签中>抓取该链接。
可能的解决方案:我还将尝试获取我的回复的整个乱码,即response.text并应用正则表达式(例如以https://...开头并以.jpg) 结尾。这肯定是大海捞针,但听起来也很实用。
谢谢!
【问题讨论】:
标签: python xpath scrapy web-crawler scrapy-splash