【问题标题】:How can I randomise User Agent for file_get_html?如何随机化 file_get_html 的用户代理?
【发布时间】:2019-08-15 04:30:15
【问题描述】:

我正在使用 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);

【问题讨论】:

  • 如果可以刮掉它并且网站所有者不介意,那么你不应该需要这个......

标签: php user-agent simpledom


【解决方案1】:

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

$headers = [
    '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',
    'header 2',
    'header n'
];
$header = $headers[array_rand($headers)];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-06-13
    • 2018-12-18
    • 2018-07-05
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多