Not all pages are created equal. When web pages are showing variations, you can use "Branch conditions" to achieve condition-based scraping. Here is how it works:
When should you consider using "Branch Conditions"?
- Hover your mouse over the where you want to add the branch condition
- Click on to add a "Branch Conditions" action inside the loop
- Click "Branch Conditions_Branch2" on the left-hand side and select "Execute if the current page contains specific element"
- Fill in the XPath for element : "//div[@class='pricing-price__savings']" into the text box below (What is XPath?).
- Click "Apply"
If you don't know how to write an XPath, you can click and select the element on the page. Octoparse would generate an XPath automatically.
- Click the branch on the right-hand side, select "Always execute the branch"
In Octoparse, you can set the condition to one of the following:
1. Always execute the branch
When this option is selected, Octoparse will not judge at all and will proceed to execute the actions within the branch immediately. Only select this option for the branch on the right side.
2. Execute if the page contains specific text
When selected, Octoparse will look for the designated text string within the current page.
3. Execute if the current page contains a specific element
When selected, Octoparse will look for the designated element (according to the XPath filled in) within the current page.
4. Execute if the current loop contains specific text
When selected, Octoparse will look for the designated text string within the current loop item.
5. Execute if the current loop contains a specific element
When selected, Octoparse will look for the designated element (according to the Relative XPath filled in) within the current loop item. Use this option only when you need to judge between items of a loop.
4. Drag the "Extract Data" action into the left branch
So now, we have configured Octoparse to look for the element on the page. If the element is found, capture the desired data, otherwise, skip the product.
1. If you want to add more conditions to classify more variations, you can click .
2. If a condition is set as "whether an element is found", the designated element must be uniquely found on the page or the judgment may fail to work.
3. Octoparse goes through the branches from left to right by default. It is important to always keep the condition you want to test for within the left branch; if the condition for the left branch is "Always execute the branch", Octoparse will not proceed to the branch on the right as "Always execute the branch" will always be tested "True".
4. You can leave the branch blank if no data extraction action is needed when the condition is not met.
5. When a data extraction action is being added to both branches, both the number of the data fields and the name of the data fields are required to be kept the same.
6. You can use nested branch judgment to further refine the test.