Wednesday, 7 December 2016

How can I get a tag's (eg. div, or other) value by parameter name

I'am new in Python and I run in a problem. There is a website where that site has a complete structure. I know how can I find a div, or other tag, but when I found that tag (eg. with class name), I would like to gathering all parameters with value, but I can't. So, My question is how can I gathering a random tag's all parameters and values after I found that?
How I found:
from bs4 import BeautifulSoup as BS
.
.
.
soup = BS(page.content, 'html.parser')
soup.prettify()
div = soup.find('div', {'class':'abc'})

-------------------------------------------------------------------------------------------------------------------------

Best Answer;



If you want to get all the element's attributes, simply use the .attrs property:
print(div.attrs)
This would print out a dictionary where keys are attribute names and values are attribute values.

insert a primary key from one table as a foreign key to another table with php lastInsertId()

I am quite a newbie in PHP and MySQL so I need some help with correcting my code.
I have two tables - person and grades First table has columns id (auto increment), namesurname. Second one has columns id (auto increment), personIdgradePersonId holds a foreign key which is a primary key as id in the person table.
I insert data into the tables with the form then have php code to handle the data and insert it into my tables:
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$grade = $_POST['grade'];

if ($firstName) {
$insertPersonSQL = <<<EOT
INSERT INTO person (firstName, lastName)
VALUES (:firstName, :lastName);
INSERT INTO grade (grade)
VALUES (:grade);
EOT;

$insertPersonStatement = $db->prepare($insertPersonSQL);
$insertPersonStatement->execute([
  ':firstName' => $firstName,
  ':lastName' => $lastName,
  ':grade' => $grade,
  ]);
}
Then I try to insert a id from person to grade as person_id using lastInsert() to get the id:
$insertIdSQL = <<<EOT
INSERT INTO grade (person_id)
VALUES (:person_id);
EOT;
$insertIDStatement = $db -> prepare($insertIdSQL);
$lastId = $db->lastInsertId();
$insertIDStatement -> execute([
  ':person_id' => $lastId,
]);
However when I try to fill the form the person_id in the table grade gets the value of 0, the rest of the columns are ok. When I debug and echo the value of $lastId it's showed correctly.
What is wrong with the code? How to correct is so that the foreign key will be inserted correctly?

-----------------------------------------------------------------------------------------------------------------------

Best Answer;



Why adding the grade without a personId? Isn't it better do do it in one query like this:
// Get params
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$grade = $_POST['grade'];

if ($firstName) {
  // insert person
  $insertPersonSQL = <<<EOT
  INSERT INTO person (firstName, lastName)
  VALUES (:firstName, :lastName);
  EOT;

  $insertPersonStatement = $db->prepare($insertPersonSQL);
  $insertPersonStatement->execute([
    ':firstName' => $firstName,
    ':lastName' => $lastName
  ]);

  // insert grade
  $insertIdSQL = <<<EOT
  INSERT INTO grade (grade, person_id)
  VALUES (:grade, :person_id);
  EOT;
  $insertIDStatement = $db -> prepare($insertIdSQL);
  $lastId = $db->lastInsertId();
  $insertIDStatement -> execute([
    ':grade' => $grade,
    ':person_id' => $lastId
  ]);
}






Powershell adding an array of different types into an array

In powershell, I am trying to add different values into an array. I am grabbing one of the values thats an int from an Array. The rest are string values. I tried + , and add( ) . Is it because they are different values. How can I add different values to an Array?
    #set up values
    $dataIdListNameNonSpecial = @{}
    $email_general = "myEmail@gmail.com"
    $name_general ="John Smith"
    $numArray = 123 , 222 ,333

    #set up temp array
    $tempArray = $numArray[ 0 ], $email_general,  $name_general

    #try to add into array
    $dataIdListNameNonSpecial += , $tempArray 

    #try to add diffent way into array
    $dataIdListNameNonSpecial.Add( $tempArray)

----------------------------------------------------------------------------------------------------------------------

Best Answer;



@{} creates a hash table, not an array. Use @() instead, and use += to add to the array.




Sign up How do I retreive a value when using the “function” syntax to perform pattern matching

The following line doesn't compile:
| IsNeither  -> sprintf "%i" // ???
Here's the function that this line belongs to:
let run = function

    | IsFizzBuzz -> "Fizz Buzz"
    | IsFizz     -> "Fizz"
    | IsBuzz     -> "Buzz"
    | IsNeither  -> sprintf "%i" // Doesn't compile
Here's the entire program: module Temp
let (|IsFizz|IsBuzz|IsFizzBuzz|IsNeither|) = function
    | n when n % 3 = 0 && 
             n % 5 = 0 -> IsFizzBuzz
    | n when n % 3 = 0 -> IsFizz
    | n when n % 5 = 0 -> IsBuzz
    | n ->                IsNeither

let run = function

    | IsFizzBuzz -> "Fizz Buzz"
    | IsFizz     -> "Fizz"
    | IsBuzz     -> "Buzz"
    | IsNeither  -> sprintf "%i" // Doesn't compile

let result = [1..16] |> List.map(run)
Can I still extract a value using the "function" syntax on the signature?
Example:
let (|IsFizz|IsBuzz|IsFizzBuzz|IsNeither|) = function

--------------------------------------------------------------------------------------------------------------- ----

Best Answer;



The easiest solution would be to make the value part of the pattern.
let (|IsFizz|IsBuzz|IsFizzBuzz|IsNeither|) = function
    | n when n % 3 = 0 && 
             n % 5 = 0 -> IsFizzBuzz
    | n when n % 3 = 0 -> IsFizz
    | n when n % 5 = 0 -> IsBuzz
    | n                -> IsNeither n

let run = function
    | IsFizzBuzz  -> "Fizz Buzz"
    | IsFizz      -> "Fizz"
    | IsBuzz      -> "Buzz"
    | IsNeither n -> sprintf "%i" n



Merge subarrays into single one and remove duplicates if they have same id using ruby on rails

I have an array of arrays like this:
array = [[1, 'Something', '123456321'], [2, 'Something', '123456321'], [2, 'Something', 1234563212']]
And I want to merge the subarrays that have same id and get this result:
array = [[1, 'Something', '123456321'], [2, 'Something, Something', '123456321, 1234563212']]
Can anyone help me? Thanks!


Best Answer;



array.group_by(&:first).map do |id, records|
  names  = records.map(&:second).join(', ')
  values = records.map(&:last).join(', ')

  [id, names, values]
end

As you asked the reversed question recently, I suggest you to read the EnumerableArrayHashand String documentations. It will give you an instant boost in expressiveness and understanding of how to do common tasks with Ruby.