Multiple items in datagrid

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Multiple items in datagrid

Post by mattys »

Im drawing a blank regarding the datagrid
I need the results from these two into the same datagrid?
  1.     $twitter = Invoke-WebRequest -Uri https://twitter.com/i/lists/1236608287036112899
  2.     $newstext = $twitter.ParsedHtml.getElementsByTagName('div') | Where-Object { $_.getAttributeNode('class').Value -eq 'js-tweet-text-container' }
  3.     $finalnews = $newstext | Select-Object textcontent
  4.    
  5.    
  6.     $twitter = Invoke-WebRequest -Uri https://twitter.com/i/lists/1236608287036112899
  7.     $tweettime = $twitter.ParsedHtml.getElementsByTagName('small') | Where-Object { $_.getAttributeNode('class').Value -eq 'time' }
  8.     $finaltime = $tweettime | Select-Object InnerText
I can get one, but not other & vice versa.. How would I get both into my grid columns? I have the columns named correctly.
  1. update-DataGridView -DataGridView $datagridview1 -Item  $finalnews, $finaltime
Any help is appreciated. Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple items in datagrid

Post by jvierra »

This will be quite hard because you are returning two unrelated collections and you will have to determine how to combine them into an object collection.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple items in datagrid

Post by jvierra »

Here is the developer ASDK/API for Twitter. It is likely the only way to do what you are trying to do unless you can determine how Twitter obfuscates the site to prevent screen scarping.

https://developer.twitter.com/en/docs/a ... lists-list
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Multiple items in datagrid

Post by mattys »

I agree, difficult.
But you made me think. Im just going to use 2 data grids and make it clean. The Twitter api sets rates on # of times within a timeframe. Scraping is giving me more room to play.

Thanks
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple items in datagrid

Post by jvierra »

Suggestions: Get the list elements and pull the items from the list element. You can get both together and create an object with the values.

The following shows obe way to do this. I tried it and it works:

Code: Select all

$twitter = Invoke-WebRequest -Uri https://twitter.com/i/lists/1236608287036112899
$lists = $twitter.ParsedHtml.getElementsByTagName('ol')
$nodes = $lists[0].childNodes
$nodes | %{
    [pscustomobject]@{
        Time = $_.getElementsByClassName('time')[0].innerText
        News = $_.getElementsByClassName('js-tweet-text-container')[0].innerText
    }
}
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Multiple items in datagrid

Post by mattys »

Wow, Fantastic Jvierra.
Thank you . It works great!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Multiple items in datagrid

Post by jvierra »

As soon as I realized it was a list the answer was trivial.

When parsing non-tabular HTML you need to find the container and then find the list components. This was easy because they used an OL/LI structure. Most sites don't make it that easy and may just use all DIVs.

I hate parsing HTML. Too much manual labor.
This topic is 4 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked