Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/39.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/8/python-3.x/18.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
Scrapy在Xpath或Css中找不到文本_Css_Python 3.x_Xpath_Web Scraping_Scrapy - Fatal编程技术网

Scrapy在Xpath或Css中找不到文本

Scrapy在Xpath或Css中找不到文本,css,python-3.x,xpath,web-scraping,scrapy,Css,Python 3.x,Xpath,Web Scraping,Scrapy,我已经在这方面做了几天了,不管我怎么努力,我都不能对一个元素中的抽象文本感到厌烦 为了节省您的所有代码,以下是重要的部分。安装程序会抓取页面上的所有内容,但不会抓取此文本 from scrapy.selector import Selector start_url = "https://www.tripadvisor.com/VacationRentalReview-g34416-d12428323-On_the_Beach_Wide_flat_beach_Sunsets_Gulf_view_S

我已经在这方面做了几天了,不管我怎么努力,我都不能对一个元素中的抽象文本感到厌烦

为了节省您的所有代码,以下是重要的部分。安装程序会抓取页面上的所有内容,但不会抓取此文本

from scrapy.selector import Selector
start_url = "https://www.tripadvisor.com/VacationRentalReview-g34416-d12428323-On_the_Beach_Wide_flat_beach_Sunsets_Gulf_view_Sharks_teeth_Shells_Fish-Manasota_Key_F.html"

#BASIC ITEM AND SPIDER YADA, SPARE YOU THE DETAILS

hxs = Selector(response)
response_css = response.css("body")

desc_data = hxs.xpath('//*[@id="DETAILS_TRUNC_TEXT"]//text()').extract()
desc_data2 = response_css.css('#DETAILS_TRUNC_TEXT::text').extract()

两者都返回空列表。是的,我通过chrome找到了xpath和css选择器,但其余的都可以正常工作,因为我可以在站点上找到其他数据。请帮助我找出这不起作用的原因。

要获取数据,您需要使用任何浏览器模拟器,如
selenium
,以便它能够捕获动态生成内容的响应。你需要把一些延迟,让网页加载它的内容完全。这就是你可以做到的:

from selenium import webdriver
from scrapy import Selector
import time

driver = webdriver.Chrome()
URL = "https://www.tripadvisor.com/VacationRentalReview-g34416-d12428323-On_the_Beach_Wide_flat_beach_Sunsets_Gulf_view_Sharks_teeth_Shells_Fish-Manasota_Key_F.html"
driver.get(URL)

time.sleep(5) #If you take out this line you won't get anything because the content of that page take some time to get loaded.

sel = Selector(text=driver.page_source)
item = sel.css('#DETAILS_TRUNC_TEXT::text').extract() #It is working
item_ano = sel.xpath('//*[@id="DETAILS_TRUNC_TEXT"]//text()').extract() #It is also working
print(item, item_ano)
driver.quit()

要获取数据,您需要使用任何浏览器模拟器,如
selenium
,以便它能够捕获动态生成内容的响应。你需要把一些延迟,让网页加载它的内容完全。这就是你可以做到的:

from selenium import webdriver
from scrapy import Selector
import time

driver = webdriver.Chrome()
URL = "https://www.tripadvisor.com/VacationRentalReview-g34416-d12428323-On_the_Beach_Wide_flat_beach_Sunsets_Gulf_view_Sharks_teeth_Shells_Fish-Manasota_Key_F.html"
driver.get(URL)

time.sleep(5) #If you take out this line you won't get anything because the content of that page take some time to get loaded.

sel = Selector(text=driver.page_source)
item = sel.css('#DETAILS_TRUNC_TEXT::text').extract() #It is working
item_ano = sel.xpath('//*[@id="DETAILS_TRUNC_TEXT"]//text()').extract() #It is also working
print(item, item_ano)
driver.quit()

我在scrapy shell中尝试了xpath和css,但也一无所获

然后我使用
view(response)
命令,发现站点是动态的

以下是一个屏幕截图:

您可以看到概览下的详细信息没有显示出来,这就是为什么无论您如何尝试,您仍然一无所获

解决方案:尝试Selenium(检查SIM在最后一个答案中提供的解决方案)或Splash


祝你好运

我在scrapy shell中尝试了xpath和css,但也一无所获

然后我使用
view(response)
命令,发现站点是动态的

以下是一个屏幕截图:

您可以看到概览下的详细信息没有显示出来,这就是为什么无论您如何尝试,您仍然一无所获

解决方案:尝试Selenium(检查SIM在最后一个答案中提供的解决方案)或Splash


祝你好运

不确定Scrapy是否正确,但selenium不会直接从xpath返回元素
//text()
,这可能是同样的问题。尝试只查找一个带有定位器的元素,就像您所做的那样
el=hxs.xpath('/*[@id=“DETAILS\u TRUNC\u TEXT”]')。extract()
并从元素中获取文本,比如
el.TEXT
,这是可能的吗?是的,TEXT()是一个只抽象文本的函数,没有该函数也无法工作。不确定scrapy,但是selenium不会直接从xpath返回元素
//text()
,这可能是同样的问题。试着像你那样找到一个带有定位器的元素,比如
el=hxs.xpath('/*[@id=“DETAILS\u TRUNC\u TEXT”]')。extract()
并从元素中获取文本,比如
el.TEXT
,这是可能的吗?是的,TEXT()是一个只提取文本的粗糙函数,没有这个函数也无法工作。谢谢。我会去飞溅路线,通过lua脚本在里面放一些假卷轴。Cheers
查看(响应)
是一个非常有用的提示!谢谢我会去飞溅路线,通过lua脚本在里面放一些假卷轴。Cheers
查看(响应)
是一个非常有用的提示!谢谢我用splash解决了这个问题,但这是解决这个问题的另一个好方法。谢谢。我用splash解决了这个问题,但这是解决这个问题的另一个好方法。