如何使用scrapy

时间:2017-03-12 03:54:05

标签: python-2.7 xpath scrapy

我必须对cat1&amp ;;执行相同的操作act2,我有谁将两个查询字符串传递给同一个Xpath提取器或组合两个查询字符串??

cat1 = response.xpath("//*[@id='linkControl']")
cat2=response.xpath("//*[@id='form1']/div[2]/div[2]/div[2]/div/div[*]/a") 

2 个答案:

答案 0 :(得分:1)

我建议您使用CSS选择器而不是Xpath。

cat1 = response.xpath("#linkControl")
cat2=response.xpath("#form1 div::nth-child(2) div::nth-child(2) div::nth-child(2) div div a") 

并将两者结合使用,(commma)

cat1 = response.xpath("#linkControl, #form1 div::nth-child(2) div::nth-child(2) div::nth-child(2) div div a")
cat2=response.xpath("#linkControl, #form1 div::nth-child(2) div::nth-child(2) div::nth-child(2) div div a") 

答案 1 :(得分:1)

XPath有union operator|)您可以使用:

xpath1 = "//*[@id='linkControl']"
xpath2 = "//*[@id='form1']/div[2]/div[2]/div[2]/div/div[*]/a"
result = response.xpath(xpath1 + " | " + xpath2)