python reptile tutorial: Based on requests modules get a request to realize Detailed

This article describes the python reptile based on requests modules get a request to achieve Hi, paper sample code described in great detail, has a certain reference value of learning for all of us to learn or work, a friend in need can refer to the following
requirements: crawling Sogou Home page data

import requests
# 1.指定url
url = 'https://www.sogou.com/'
# 2.发起get请求:get方法会返回请求成功的响应对象
response = requests.get(url=url)
# 3.获取响应中的数据:text属性作用是可以获取响应对象中字符串形式的页面数据
page_data = response.text
# 4.持久化数据
with open("sougou.html","w",encoding="utf-8") as f:
  f.write(page_data)
  f.close()
print("ok")

How to deal with requests module carrying a get request parameters, return request carries parameters of
demand: a designated entry, data acquisition Sogou search results page corresponding to

Before the url parameters of urllib Chinese coding processing requires processing, requests will be processed automatically encoded url

Initiated with arguments get request
params can preach dictionary or list

def get(url, params=None, **kwargs):
  r"""Sends a GET request.
  :param url: URL for the new :class:`Request` object.
  :param params: (optional) Dictionary, list of tuples or bytes to send
    in the body of the :class:`Request`.
  :param \*\*kwargs: Optional arguments that ``request`` takes.
  :return: :class:`Response <Response>` object
  :rtype: requests.Response
import requests
# 指定url
url = 'https://www.sogou.com/web'
# 封装get请求参数
prams = {
  'query':'周杰伦',
  'ie':'utf-8'
}
response = requests.get(url=url,params=prams)
page_text = response.text
with open("周杰伦.html","w",encoding="utf-8") as f:
  f.write(page_text)
  f.close()
print("ok")

Module requests using custom request header information, parameters and initiates the get request

get method has the request header information of the headers parameter assigned to the headers parameter dictionaries

import requests
# 指定url
url = 'https://www.sogou.com/web'
# 封装get请求参数
prams = {
  'query':'周杰伦',
  'ie':'utf-8'
}
# 自定义请求头信息
headers={
  'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
  }
response = requests.get(url=url,params=prams,headers=headers)
page_text = response.text
with open("周杰伦.html","w",encoding="utf-8") as f:
  f.write(page_text)
  f.close()
print("ok")

We recommend the python learning sites, [点击进入](https://jq.qq.com/?_wv=1027&k=5JIjRvv)to see how old the program is to learn! From basic python script, reptiles, django, data mining, programming techniques, work experience, as well as senior careful study of small python partners to combat finishing zero-based information projects! Every day Python programmers explain the timing of technology, sharing some learning methods and the need to pay attention to small details
is more than the entire contents of this article, we want to help learning

Released seven original articles · won praise 1 · views 3556

Guess you like

Origin blog.csdn.net/haoxun12/article/details/104954688