Scrape a webpage for image and add it to post

For all my answers I’m assuming you both have the legal rights to use those images and are aware of any implications of duplicate content (SEO etc). That being said there are definitely ways.

At what point would you like to scrape images? Regularly? If so look into WP_Cron and automate it.

If you just want to scrape the image once you generate a new post, you could add your custom code to the save_post hook. However you still need to specify which URL to scrape from. For this you could add a Custom Metabox with an URL field.

For the actual scraping of the page you should look into DOMDocument which makes handling a rendered HTML page with PHP pretty easy. You could use DOMDocument::getElementsByTagName to find an image tag. You could then use PHP’s image function to check for a certain minimum size or other criteria that makes this image the “best” image.

Once you have the image you can upload it into the WordPress media library using wp_handle_upload (I don’t have enough reputation to post another link). The only thing left then is to set your image to the featured image. Again not enough reputation to post another link, but a quick Google search will lead to results on how to do that.