cat1で同じ操作を実行する必要があります& act2、両方のクエリ文字列を同じXpath抽出プログラムに渡すか、両方のクエリ文字列を組み合わせるのですか?2つのクエリ文字列をレスポンスで使用する方法
cat1 = response.xpath("//*[@id='linkControl']")
cat2=response.xpath("//*[@id='form1']/div[2]/div[2]/div[2]/div/div[*]/a")
cat1で同じ操作を実行する必要があります& act2、両方のクエリ文字列を同じXpath抽出プログラムに渡すか、両方のクエリ文字列を組み合わせるのですか?2つのクエリ文字列をレスポンスで使用する方法
cat1 = response.xpath("//*[@id='linkControl']")
cat2=response.xpath("//*[@id='form1']/div[2]/div[2]/div[2]/div/div[*]/a")
XPathはあなたが使用することができますunion operator(|
)が使用結合する:
xpath1 = "//*[@id='linkControl']"
xpath2 = "//*[@id='form1']/div[2]/div[2]/div[2]/div/div[*]/a"
result = response.xpath(xpath1 + " | " + xpath2)
を使用でき
はい、うまくいきました! –
Firebugでテストするとうまくいきますが、scrapyコードではxpath1にのみ対応しています。 提案はありますか? –
Xpathの代わりにCSSセレクタを使用することをお勧めします。
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")
またはオペレータを '|'正規表現で – stamaimer
HTMLコードを投稿 –
@stamaimer私はこれをlkeに使うはずですか?response.xpath( "// * [@ id = 'linkControl']" | "// * [@ id = 'form1']/div [2]/div [2]/div/div [*]/a ") –