代码拉取完成,页面将自动刷新
from __future__ import print_function
import re
import mechanize
import urllib2
br = mechanize.Browser()
br.addheaders = [('User-Agent',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36')]
br.set_handle_robots(False)
# For page exploration
page = input('Enter Page No:')
# print type(page)
p = urllib2.Request('https://www.google.co.in/search?q=gate+psu+2017+ext:pdf&start=' + page)
ht = br.open(p)
text = '<cite\sclass="_Rm">(.+?)</cite>'
patt = re.compile(text)
h = ht.read()
urls = re.findall(patt, h)
int = 0
while int < len(urls):
urls[int] = urls[int].replace("<b>", "")
urls[int] = urls[int].replace("</b>", "")
int = int + 1
print(urls)
for url in urls:
try:
temp = url.split("/")
q = temp[len(temp) - 1]
if "http" in url:
r = urllib2.urlopen(url)
else:
r = urllib2.urlopen("http://" + url)
file = open('psu2' + q + '.pdf', 'wb')
file.write(r.read())
file.close()
print("Done")
except urllib2.URLError as e:
print("Sorry there exists a problem with this URL Please Download this Manually " + str(url))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。