爬虫板子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def askURL(url):
head={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36"
}
request=urllib.request.Request(url,headers=head)

try:
response=urllib.request.urlopen(request)
html=response.read().decode("utf-8")
return html
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)

url="http://12345.chengdu.gov.cn/searchTelDeal?telID=21221096&class=100&WorkFPkId=6951786"
html=askURL(url)
print(html)
f=open('output1.html', 'w', encoding='utf-8')
f.write(html)
print('成功')