爬取第一步

www.dan發表於2020-10-21

1、第一開啟網頁》F12》network》停止,》檢視headers》response headers中User-agent複製
2、

import urllib.request
import urllib.parse
#第一個表達
# headers={
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
# }
# url='http://httpbin.org.post'
# data=bytes(urllib.parse.urlencode({'name':'eric'}),encoding='utf-8')
# req=urllib.request.Request(url=url,data=data,headers=headers,method='POST')
# response=urllib.request.urlopen(req)
# print(response.read().decode('utf-8'))




#第二個表達
#url='https://www.douban.com'
# headers={
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"
# }
# req=urllib.request.Request(url=url,headers=headers)
# response=urllib.request.urlopen(req)
# print(response.read().decode('utf-8'))

相關文章