0
$\begingroup$

I am trying to get a number from a plaintext with the staring sign of "Nho xanh không hạt" and ending sign of "đ" automatically with different method but it doesn't work. In detail, I want to get the number "99.500" from the first text and "275,000" from the second text as:

textin = "Nho xanh không hạt"

url2a = "https://www.bachhoaxanh.com"
url2b = "https://nongsandungha.com"

result2a = 
 WebSearch[textin, "Snippets", "Site" -> url2a, Method -> "Google"]

result2b = 
 WebSearch[textin, "Snippets", "Site" -> url2b, Method -> "Google"]

StringCases[result2a, textin ~~ _ ~~ "đ"]

StringCases[result2b, textin ~~ _ ~~ "đ"]

TextCases[result2a, "CurrencyAmount" -> "Interpretation"]

TextCases[result2b, "CurrencyAmount" -> "Interpretation"]

StringCases[[result2a, 
  textin ~~ _ ~~ StringEndsQ[#, "đ"] &], DigitCharacter]

StringCases[[result2b, 
  textin ~~ _ ~~ StringEndsQ[#, "đ"] &], DigitCharacter]

Could you please suggest me how to obtain the above number? Thank you in advance.

$\endgroup$
4
  • $\begingroup$ Try result2a // StringCases[(DigitCharacter | ".") .. ~~ "₫"] // DeleteCases[{}]. $\endgroup$ Oct 22, 2021 at 1:42
  • $\begingroup$ Please load the relevant (shortened) part of the text that you have as a result {of one of the web searches preferably} so that your problem is limited to a string search problem only. You can also edit your post to remove the supplementary details, to get focused replies. $\endgroup$
    – Syed
    Oct 22, 2021 at 6:02
  • $\begingroup$ @RohitNamjoshi Thank you for your useful comments. I have tried and face a problem and don't know how to solve. Please advise the problems as below: "ServiceExecute::nval: Invalid value for parameter Language in service GoogleCustomSearch"; "WebSearch["Nho xanh không hạt", "Snippets", "Site" -> "bachhoaxanh.com", Method -> "Google"]" they are totally strange I can't fix the problems. $\endgroup$
    – Anh
    Oct 22, 2021 at 6:13
  • $\begingroup$ @Syed yesterday I can find the full plaintext, not the interested number. But today I can't find the text with following message "ServiceExecute::nval: Invalid value for parameter Language in service GoogleCustomSearch"; "WebSearch["Nho xanh không hạt", "Snippets", "Site" -> "bachhoaxanh.com", Method -> "Google"]". I don't know how to fix the problem. Please advices $\endgroup$
    – Anh
    Oct 22, 2021 at 6:17

1 Answer 1

1
$\begingroup$

Note that many websites will not let you have their plaintext easily, if at all. There are all sorts of wrappers around useful information. That's why search is a lucrative business. Having said that, if a string search is your problem and you are going with the plaintext route; try the following example (without going through any company's server as there will always be some limit that will kick in sooner or later on what you can do there):

ptext = Import["https://www.bachhoaxanh.com", "Plaintext"]

The resulting text has many entries of the form:

    Rau cải bẹ xanh 4KFarm gói 500g  
  11.000₫   15.000₫   -27%     Chọn mua  

   
       Rau cải thìa tươi 4KFarm gói 500g  
  11.000₫   15.000₫   -27%     Chọn mua  
 
 
Xem trực tiếp         
Khoai lang Nhật dẻo ngọt túi 1kg  
  35.000₫     Chọn mua  
 
      
Táo Gala mini nhập khẩu hộp 1kg  
  49.000₫     Chọn mua

I believe the last word "Chọn mua" stands for "buy". As an example, let's try to get one of these products.

astr = StringCases[ptext, 
  "Rau cải bẹ xanh" ~~ Shortest[___] ~~ "Chọn mua "]
{"Rau cải bẹ xanh 4KFarm gói 500g  
  11.000₫   15.000₫   -27%     Chọn mua "}

For the interested reader, this is a list with string(s) inside it.

tcases = TextCases[#, "CurrencyAmount" -> "Interpretation"] & /@ 
   astr // Flatten[#, 1] &

{Quantity[11., "VietnameseDong"], Quantity[15., "VietnameseDong"]}

You can extract the first quantity with First@tcases.


If you want to extract (let's say 500g, for products with different weights) it is often not so easy. My first attempt is as follows:

wcases = StringCases[#, DigitCharacter .. ~~ LetterCharacter ..] & /@ 
   astr // Flatten[#, 1] &

{"4KFarm", "500g"}

The way data is presented will change from website to website. Often it is not easy to get the data without tailored attempts.

I hope this helps you. I have no knowledge of Vietnamese so please do forgive me the lapses.

$\endgroup$
3
  • $\begingroup$ Thank @Syed. Your code is helpful for the good on the main domain. The "Nho xanh không hạt" is in the subdomain. I even put exact subdomains bachhoaxanh.com/trai-cay-tuoi-ngon/nho-xanh-khong-hat-tui-500g and nongsandungha.com/thuc-pham/nho-xanh-khong-hat-my for the "Nho xanh không hạt" but the command does't work. Could you please suggest to find the number in the subdomain by searching on the subdomain or on the main domain ? $\endgroup$
    – Anh
    Oct 22, 2021 at 12:26
  • $\begingroup$ Try to get "Hyperlinks" from the subdomains instead of "Plaintext" that you are interested in. I cannot help you further since I am not familiar with the language. If you reach the subdomain however, you will know how to extract information from strings. Best of luck. $\endgroup$
    – Syed
    Oct 22, 2021 at 12:45
  • $\begingroup$ Thank you for your comments $\endgroup$
    – Anh
    Oct 27, 2021 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.