如何随机分配file_get_html的用户代理?

时间:2019-03-24 22:30:43

标签: php user-agent simpledom

我正在抓取具有simple_html_dom的网站(毫无疑虑),并且需要随机分配用户代理。

尝试了多个数组的内容,但继续获取第一个。

$opts = array(
     'http'=>array(
       'header'=>"User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53\r\n"
     )
   );
$context = stream_context_create($opts);
$html = file_get_html($webpage, false, $context);

1 个答案:

答案 0 :(得分:2)

将此代码添加到您的代码上方,并在$ opts中使用$ header

<?php
 //create connection vars
 $dbhost = '-';
 $dbuser ='-';
 $dbpass = '-';
 $db = '-';
 //create connection
 $con=mysql_connect($dbhost, $dbuser, $dbpass);
 mysql_select_db($db);

 switch($_GET['Allergen1']){
    case 'Wheat':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Wheat = 0");
       break;
    case 'Spelt':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Spelt = 0");
       break;
    case 'Kamut':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Kamut = 0");
       break;
    case 'Rye':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Rye = 0");
       break;
    case 'Barley':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Barley = 0");
       break;
    case 'Oats':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Oats = 0");
       break; 
    case 'Fish':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Fish = 0");
       break;
    case 'Crustaceans':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Crustaceans = 0");
       break;
    case 'Molluscs':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Molluscs = 0");
       break;
    case 'Eggs':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Eggs = 0");
       break;
    case 'Soybeans':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Soybeans = 0");
       break;
    case 'Milk':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Milk = 0");
       break;
    case 'Almonds':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Almonds = 0");
       break;
    case 'Hazelnut':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Hazelnut = 0");
       break;
    case 'Walnut':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Walnut = 0");
       break;
    case 'CashewNut':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE CashewNut = 0");
       break;
    case 'PecanNut':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE PecanNut = 0");
       break;
    case 'BrazilNut':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE BrazilNut = 0");
       break;
    case 'Pistacio':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Pistacio = 0");
       break;
    case 'Macadamia':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Macadamia = 0");
       break;
    case 'Peanuts':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Peanuts = 0");
       break;
    case 'Celery':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Celery = 0");
       break;
    case 'Mustard':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Mustard = 0");
       break;
    case 'Sesame':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Sesame = 0");
       break;
    case 'Sulphites':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Sulphites = 0");
       break;
    case 'Lupin':
       mysql_query("SELECT DishName FROM AllergenDishMenu WHERE Lupin = 0");
       break;
    }
?>


 <!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="Styling.css">
<meta charset="utf-8">
<title>Allergen Menu</title>
</head>
<body>

  <h1>Allergen Menu</h1>


  <div id ="container">
    <span id ="tableleft">
        <!-- NEED TO ECHO RESULT OF THE QUERY HERE BASED UPON THE SELECT OPTION FROM THE FORM BELOW !-->
    </span>
    <span id ="tableright">
        <Form method="post">
            <SELECT name="Allergen1">
                <option value="Wheat">Wheat</option>
                <option value="Spelt">Spelt</option>
                <option value="Kamut">Kamut</option>
                <option value="Rye">Rye</option>
                <option value="Barley">Barley</option>
                <option value="Oats">Oats</option>
                <option value="Fish">Fish </option>
                <option value="Crustaceans">Crustaceans</option>
                <option value="Molluscs">Molluscs</option>
                <option value="Eggs">Eggs</option>
                <option value="Soybeans">Soybeans</option>
                <option value="Milk">Milk</option>
                <option value="Almonds">Almonds</option>
                <option value="Hazelnut">Hazelnut</option>
                <option value="Walnut">Walnut</option>
                <option value="CashewNut">Cashew Nut</option>
                <option value="PecanNut">Pecan Nut</option>
                <option value="BrazilNut">Brazil Nut</option>
                <option value="Pistacio">Pistacio</option>
                <option value="Macadamia">Macadamia</option>
                <option value="Peanuts">Peanuts</option>
                <option value="Celery">Celery</option>
                <option value="Mustard">Mustard</option>
                <option value="Sesame">Sesame</option>
                <option value="Sulphites">Sulphites</option>
                <option value="Lupin">Lupin</option>
            </SELECT>
            <input type="submit">
        </Form>
    </span>
  </div>
  </html> 
</body>