מניפולציה על דף אינטרנט ישירות דרך html, תוך שימוש בrselenium

50 views
Skip to first unread message

David Harar

unread,
May 15, 2019, 8:27:10 AM5/15/19
to Israel R User Group
שלום לכולם, מצטער על הכותרת הלא קליטה, אבל היא משקפת את הבעיה המאוד ספציפית שבה נתקלתי.

לשם עבודה אקדמית אני מנסה להשיג מידע ציבורי של נתוני רייטינג יומיים.
הגוף שאוסף את הנתונים ומעלה אותם לאינטרנט הוא הוועדה הישראלית למדרוג.
באתר הזה נמצאים דוחות הוועדה היומיים. מדובר על אתר דינאמי, שבו עלינו להזין נתונים במספר שדות (אוכלוסיה, שבועי\יומי ותאריך) וללחוץ על "הצג טבלה" - הליך שאפשר להפוך לאוטומטי באמצעות rselenium. הבעיה היא ששדה התאריך אינו מגיב כלל ל-chrome ול-firefox, אלא רק ל-internet explorer. דרך כרום, ניתן לשנות את התאריך הרצוי רק באמצעות שינוי הערך בשורת ה-html עצמה, כפי שמתואר בתמונה הבאה:

Untitled.png

אני עובד על docker hub מבוסס chrome, והשאלה שלי היא האם יש דרך להפוך את התהליך של הזנת התאריכים ב-html לאוטומטי?

תודה רבה,
דוד

Michael Dorman

unread,
May 15, 2019, 9:35:51 AM5/15/19
to israel-r-...@googlegroups.com
ניסיתי אסטרטגיה אחרת ונראה שזה עובד. 

1. הלכתי ללשונית Network שם מופיעה הבקשה של טעינת טבלה. אפשר להעתיק אותה עם Copy as cURL - 

Screenshot from 2019-05-15 16-30-13.png

2. את הבקשה אפשר להעתיק לשורת פקודה על מנת לטעות את ה-HTML של טבלה מסויימת עם curl ולמשל לשמור בקובץ (או לקרוא לתוך R) - 

Screenshot from 2019-05-15 16-32-26.png

3. אחד הפרמטרים של הבקשה הוא התאריך, אפשר לערוך את הבקשה כדי לקבל תאריכים שונים - 

Screenshot from 2019-05-15 16-32-43.png

ממבט ראשון התוצאה נראית נאמנה למקור כאשר פותחים בדפדפן - 

Screenshot from 2019-05-15 16-28-10.png


--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/israel-r-user-group/932ed348-6593-4dc6-925d-2f17f2a23dff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Message has been deleted

David Harar

unread,
May 16, 2019, 1:51:52 AM5/16/19
to Israel R User Group
היי מיכאל,
ראשית, תודה רבה על התשובה המפורטת, עזרת לי מאוד! בהתחלה קצת הסתבכתי כי זו הפעם הראשונה שיוצא לי לעבוד עם cURL.
כיצד ניתן להכניס את כתיבת הבקשה ללופ? האם ניתן לכתוב את הבקשה הזו דרך הr, כמו בshell?
שוב תודה

Michael Dorman

unread,
May 16, 2019, 3:36:50 AM5/16/19
to israel-r-...@googlegroups.com

היי דוד,

(1) אם מותקן אצלך במחשב curl, אפשר להפעיל גם מתוך R בעזרת פונקציית system

לדוגמה - 

x = system("curl 'http://212.150.52.211/scripts/rating10.asp' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: http://212.150.52.211' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Referer: http://212.150.52.211/scripts/public1.asp' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -H 'Cookie: ASPSESSIONIDASTRCSAC=AJDJLICCCADDPFNAKIPGGNBB' --data 'audience=1&week=05%2F05%2F2019&date_selector=weekly&txtFromDateDAILY=15%2F05%2F2019&1=View+Table+%2F+%E4%F6%E2+%E8%E1%EC%E4&weekCount=1' --compressed", intern = TRUE)

(2) אפשר לעשות את אותו הדבר דרך R, יש מספר ספריות שיכולות לשלוח בקשת HTTP, למשל httr

לדוגמה - 

library(httr)
x = POST("http://212.150.52.211/scripts/rating10.asp", add_headers("Connection" = "keep-alive", "Cache-Control" = "max-age=0", "Origin" = "http://212.150.52.211", "Upgrade-Insecure-Requests" = "1", "Content-Type" = "application/x-www-form-urlencoded", "User-Agent" = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36", "Accept" = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3", "Referer" = "http://212.150.52.211/scripts/public1.asp", "Accept-Encoding" = "gzip, deflate", "Accept-Language" = "en-US,en;q=0.9", "Cookie" = "ASPSESSIONIDASTRCSAC=AJDJLICCCADDPFNAKIPGGNBB"), body = "audience=1&week=05%2F05%2F2019&date_selector=weekly&txtFromDateDAILY=15%2F05%2F2019&1=View+Table+%2F+%E4%F6%E2+%E8%E1%EC%E4&weekCount=1", encode = "form")
x = content(x, as = "text", type = "text/html", encoding = "WINDOWS-1255")

בשיטה (1) התוצאה היא בדיוק כמו להפעיל את curl בשורת הפקודה כמו ששלחתי בצילום מסך אתמול. 

בשיטה (2) הטקסט העברי יוצא בג'יבריש בדפדפן - 
Screenshot from 2019-05-16 10-31-28.png

אבל ב-R הטקסט העברי נראה תקין ואפשר להוציא ממנו את הנתונים ישירות ב-R - 
Screenshot from 2019-05-16 10-34-13.png

אני מעדיף את שיטה (1) כי יש הרבה יותר מידע באינטרנט על curl מאשר על ספריות R שמפעילות את curl או ספריות דומות בעקיפין. 

אפשר להכניס את אחד הקודים הנ"ל ללופ בדיוק כמו כל קוד אחר ב-R, ובכל פעם לשנות רק את החלק של התאריך.  




--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
Message has been deleted

David Harar

unread,
May 16, 2019, 9:50:02 AM5/16/19
to Israel R User Group
היי מיכאל,
בהמלצתך אני נוטה להשתמש בשיטה הראשונה. 
עם זאת, למרות שהתקנתי curl ו-git gui בנפרד, אני עדיין מקבל שגיאה כאשר אני מריץ את הקוד:
x = system("curl 'http://212.150.52.211/scripts/rating10.asp' -H 'Connection: keep-alive' -H 'Cache-Control: max-age=0' -H 'Origin: http://212.150.52.211' -H 'Upgrade-Insecure-Requests: 1' -H 'Content-Type: application/x-www-form-urlencoded' -H 'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' -H 'Referer: http://212.150.52.211/scripts/public1.asp' -H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.9' -H 'Cookie: ASPSESSIONIDASTRCSAC=AJDJLICCCADDPFNAKIPGGNBB' --data 'audience=1&week=05%2F05%2F2019&date_selector=weekly&txtFromDateDAILY=15%2F05%2F2019&1=View+Table+%2F+%E4%F6%E2+%E8%E1%EC%E4&weekCount=1' --compressed", intern = TRUE)
ערך ה-x שאני מקבל נראה כך

Untitled.png

ובאלמנט הראשון של x כתוב
curl: option --compressed: the installed libcurl version doesn't support this.
יצא לך להתקבל בבעיה כזו?

שוב תודה

Michael Dorman

unread,
May 16, 2019, 9:52:40 AM5/16/19
to israel-r-...@googlegroups.com
אולי בגרסה של curl שמותקנת אצלך אין את האופציה הזו, הייתי מנסה ללא "compressed--"

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.

David Harar

unread,
May 17, 2019, 1:38:40 AM5/17/19
to Israel R User Group
זה מוזר כי ב-git bash אני כן מצליח להשתמש בפונקציה compress--, וגם אחרי שהורדתי curl בנפרד עדיין קיבלתי את ההודעה ההיא ב-r שאומרת שהגרסה הנוכחית של ה-curl אינה מסוגלת להריץ את הפונקציה. 
מה שעשיתי בסוף היה להגדיר לופ שיכתוב לי את השמות של שורות הקוד הרלוונטיות, לעשות להן print() על מנת שיופיעו בקונסולה, ואת הפלט שמרתי באמצעות קובץ טקסט (באמצעות sink). זה נתן לי מסמך עם כל שמות הבקשות שאני צריך לשלוח לאתר שלהם (הבנתי נכון?), והעתקתי את זה בצ'אנקים לתוך ה-git bash. זה לקח איזה שעה, אבל עכשיו יש לי את כל ה-htmlים בתיקייה, מהם כבר אוכל להוריד ולסדר את הנתונים דרך ה-rvest. 
תודה מיכאל, עזרת מאוד!
אגב, ב-git bash עצמו, קיימת פונקציה של המתנה? לא הצלחתי למצוא מידע על זה באינטרנט.

Michael Dorman

unread,
May 17, 2019, 5:39:30 AM5/17/19
to israel-r-...@googlegroups.com
שמח לשמוע שזה עזר!

לא מכיר את ה-git bash, אבל ב-bash רגיל אפשר להפעיל המתנה עם sleep - 
דרך אגב, במקום print ו-sink ב-R אפשר לכתוב וקטור של שורות לתוך קובץ עם writeLines

ואם יש לך קובץ טקסט בשם filename שמכיל פקודות bash, אז אפשר להריץ אותו עם הפקודה bash filename, במקום להעתיק ולהדביק את הפקודות - 

--
You received this message because you are subscribed to the Google Groups "Israel R User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to israel-r-user-g...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages