Python爬虫-requests基础,实战-第一血
import requests
if __name__ == "__main__":
url = 'https://www.sogou.com/'
response = requests.get(url=url)
page_text = response.text
print(page_text)
with open('./sogou.html', 'w',encoding='utf-8')as fp:
fp.write(page_text)
print("爬取完毕")
requests实战之破解百度翻译.py
import json
import requests
if __name__ == "__main__":
post_url = 'https://fanyi.baidu.com/sug'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31'
}
word = input("enter a word:")
data = {
'kw':word
}
response = requests.post(url=post_url,data=data,headers=header)
dic_obj = response.json()
fileName = word + '.json'
fp = open(fileName,'w', encoding='utf-8')
json.dump(dic_obj,fp=fp,ensure_ascii=False)
print("over!")
requests词条搜索.py
import requests
if __name__ == "__main__":
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31'
}
url = 'https://www.sogou.com/web'
kw = input('enter a word:')
param = {
'query': kw
}
response = requests.get(url=url, params=param)
page_text = response.text
fileName = kw + '.html'
with open(fileName, 'w', encoding='utf-8')as fp:
fp.write(page_text)
print(fileName, "保存成功!!!")
requests实战之肯德基餐厅位置爬取.py
import requests
if __name__ == "__main__":
url = 'http://www.kfc.com.cn/kfccda/storelist/index.aspx'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31'
}
e = int(input('enter a pageSize:'))
for i in range(e):
pageIndex = ""
par = {
'cname':'',
' pid':'',
'keyword': '北京',
'pageIndex': i,
'pageSize': e,
}
response = requests.post(url=url, data=par, headers=header)
kedeji_text = response.text
print(kedeji_text)
with open('./kedeji.text','w', encoding='utf-8')as fp:
fp.write(kedeji_text)
print("读取完毕!!")
requests实战之爬取药监总局数据.py
import requests
import json
if __name__ == '__main__':
url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.61 Safari/537.36 Edg/94.0.992.31'
}
id_list = []
all_data_list = []
for i in range(1,6):
page = str(i)
data = {
'on': 'true',
'page': page,
'pageSize': '15',
'productName':'',
'conditionType': '1',
'applyname':'',
'applysn':'',
}
json_ids = requests.post(url=url, headers=header, data=data).json()
for dic in json_ids['list']:
id_list.append(dic['ID'])
post_url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
for id in id_list:
data = {
'id':id
}
data_json = requests.post(url=post_url,headers=header,data=data).json()
all_data_list.append(data_json)
fp = open('./allData.json','w',encoding='utf-8')
json.dump(all_data_list,fp=fp,ensure_ascii=False)
print('over!!!')
print("爬取完毕")
step1:指定url
- url = ‘https://www.sogou.com/’
step2: 发起请求
# get方法会返回一个响应对象
response = requests.get(url=url)
step3:获取响应数据。text返回是字符串形式数据
page_text = response.text
print(page_text)
step4:持久化保存数据
|