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.
Locked
mattys
Posts: 20
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Mon Dec 30, 2019 10:58 am

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.  
  3.     $newstext = $twitter.ParsedHtml.getElementsByTagName('div') | Where-Object { $_.getAttributeNode('class').Value -eq 'js-tweet-text-container' }
  4.  
  5.     $finalnews = $newstext | Select-Object textcontent
  6.  
  7.    
  8.  
  9.    
  10.  
  11.     $twitter = Invoke-WebRequest -Uri https://twitter.com/i/lists/1236608287036112899
  12.  
  13.     $tweettime = $twitter.ParsedHtml.getElementsByTagName('small') | Where-Object { $_.getAttributeNode('class').Value -eq 'time' }
  14.  
  15.     $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: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 20
Joined: Mon Dec 30, 2019 10:58 am

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: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 20
Joined: Mon Dec 30, 2019 10:58 am

Re: Multiple items in datagrid

Post by mattys »

Wow, Fantastic Jvierra.
Thank you . It works great!

jvierra
Posts: 14444
Joined: Tue May 22, 2007 9:57 am
Contact:

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.

Locked