# coding=utf-8
import requests
# 请求远程图片的函数,参数url为图片的完整链接,函数返回请求回来的二进制内容
def get_content(url):
# 准备好User-Agent到变量myheaders
myheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'}
response = requests...
# coding=utf-8
# 覆盖写入
def save_to_file(file_name, content):
with open(file_name, 'w', encoding='utf-8') as f:
f.write(content)
if __name__ == '__main__':
save_to_file('./test.txt', '这是要写入的内容')
# 循环写入
with open('./test.txt', 'a', encoding='utf-8') as f:
for i in range(0,10):
f.write(str...
# coding=utf-8
import requests
def post_payload(url, payload):
myheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'}
response = requests.post(url, headers=myheaders, data=payload)
status_code = response.s...
# coding=utf-8
import requests
# 请求网页的函数,参数url为网页的完整链接,函数返回请求回来的HTML代码
def get_html(url):
# 准备好User-Agent到变量myheaders
myheaders = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36'}
response = requests.get(...
# coding=utf-8
import requests
html = requests.get('http://www.spiderbuf.cn/list').text
print(html)
# 在Windows环境下,Python爬取网页出现乱码通过是因为Windows环境默认编码是GBK而大部分网页编码是UTF-8
# 此时可以利用requests库的content方法,它会自动根据网页编码进行转换
# requests避免乱码的爬虫代码修改如下:
import requests
html_bytes = requests.get('http://www.spiderbuf.c...