我有一个cURL

curl 'http://www.gocomics.com/search?search_string=garfield' -H 'Host: www.gocomics.com' -H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Cookie: _GoComics3_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTI3YTQ1NzY3NmJjMjJhYWUwNDFjODNjMjViYzY1MmQyBjsAVEkiDnJldHVybl90bwY7AEYiIy9zZWFyY2g%2Fc2VhcmNoX3N0cmluZz1nYXJmaWVsZEkiEF9jc3JmX3Rva2VuBjsARkkiMTBzRjhMaWlLb3lTRWM5Y05naUJnTFZhMmZKaEhXTENPM29nSms3STVrY2c9BjsARg%3D%3D--1ec4e3b379629daf62427da9b4bbe7466b71048e; _ga=GA1.2.1017055044.1433399187; __atuvc=6%7C22; __atuvs=556fef9281e9c25a005; __atssc=google%3B2; __qca=P0-1022399293-1433399204316; _gat_trackerUU4=1' -H 'Connection: keep-alive' -H 'If-None-Match: "d7a8fc41bab9afc05623db6a7741ef11"'


如何在Python中将所有参数放入urllib请求中?

最佳答案

import urllib2

req = urllib2.Request('http://www.gocomics.com/search?search_string=garfield')
req.add_header('Host', 'Host: www.gocomics.com')
req.add_header('User-Agent', 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0')
req.add_header('Accept', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
req.add_header('Accept-Language', 'Accept-Language: en-US,en;q=0.5')
req.add_header('Cookie', 'Cookie: _GoComics3_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTI3YTQ1NzY3NmJjMjJhYWUwNDFjODNjMjViYzY1MmQyBjsAVEkiDnJldHVybl90bwY7AEYiIy9zZWFyY2g%2Fc2VhcmNoX3N0cmluZz1nYXJmaWVsZEkiEF9jc3JmX3Rva2VuBjsARkkiMTBzRjhMaWlLb3lTRWM5Y05naUJnTFZhMmZKaEhXTENPM29nSms3STVrY2c9BjsARg%3D%3D--1ec4e3b379629daf62427da9b4bbe7466b71048e; _ga=GA1.2.1017055044.1433399187; __atuvc=6%7C22; __atuvs=556fef9281e9c25a005; __atssc=google%3B2; __qca=P0-1022399293-1433399204316; _gat_trackerUU4=1')
req.add_header('Connection', 'Connection: keep-alive')
req.add_header('If-None-Match', 'If-None-Match: "d7a8fc41bab9afc05623db6a7741ef11"')

resp = urllib2.urlopen(req)
content = resp.read()

10-07 22:23