Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intelligent screen scraping using different proxies and user-agents randomly?

I want to download few HTML pages from http://abc.com/view_page.aspx?ID= The ID is from an array of different numbers.

I would be interested in visiting multiple instances of this URL and saving the file as [ID].HTML using different proxy IP/ports.

I want to use different user-agents and I want to randomize the wait times before each download.

What is the best way of doing this? urllib2? pycURL? cURL? What do you prefer for the task at hand?

Please advise. Thanks guys!

like image 812
ThinkCode Avatar asked May 10 '10 15:05

ThinkCode


People also ask

How many proxies do I need for scraping?

In order to figure out the number of proxy servers you need then, you can divide the total throughput of your web scraper (number of requests per hour) by the threshold of 500 requests per IP per hour to approximate the number of different IP addresses you'll need.

What is User Agent scraping?

A user agent is a computer program representing a person, for example, a browser in a Web context. Besides a browser, a user agent could be a bot scraping webpages, a download manager, or another app accessing the Web.

What is proxy scraping?

A proxy service for scraping is used to manage proxies for a scraping project. A simple proxy service for scraping could simply be a set of proxies that are used in parallel to create the appearance of separate users accessing the site at the same time.


2 Answers

Use something like:

import urllib2
import time
import random

MAX_WAIT = 5
ids = ...
agents = ...
proxies = ...

for id in ids:
    url = 'http://abc.com/view_page.aspx?ID=%d' % id
    opener = urllib2.build_opener(urllib2.ProxyHandler({'http' : proxies[0]}))
    html = opener.open(urllib2.Request(url, None, {'User-agent': agents[0]})).read()
    open('%d.html' % id, 'w').write(html)
    agents.append(agents.pop()) # cycle
    proxies.append(proxies.pop())
    time.sleep(MAX_WAIT*random.random())
like image 144
hoju Avatar answered Oct 23 '22 16:10

hoju


Use unix tool wget. It has option to specify custom user-agent and delay between each retrieval of the page.

You can see wget(1) man page for more information.

like image 30
pajton Avatar answered Oct 23 '22 16:10

pajton