Python 学习随笔

这篇博客是作者学习Python爬虫过程的个人笔记,包括2020年4月和5月遇到的问题,如print响应换行、设置User-Agent爬取猫眼电影排行榜,以及使用GUI后运行命令行弹窗的问题。博主欢迎留言交流,共同学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人兴趣,开始学习python,主要是爬虫相关编程,当然只是随笔,记录学习过程中遇到的不解,问题和解决方法,如果疑问欢迎留言,共同学习,共同记录(持续更新)

#1.20200421-print输入respone不能换行的问题

import urllib.parse
import urllib.request

data = bytes(urllib.parse.urlencode({'word':'hello'}),encoding='utf8')
respone = urllib.request.urlopen('http://httpbin.org/post', data=data)
#print(respone.read())
print(respone.read().decode('utf-8'))//只是需要在这里添加解码并可以换行输出

#2.20200422-爬取猫眼电影排行版需要User-Agent

import requests
from requests.exceptions import RequestException
import re

def get_one_page(url):
    try:
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'
        }
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            return response.text
        return None
    except RequestException:
        return None

url= 'http://maoyan.com/board/4'
html = get_one_page(url)
print(html)

#2.20200520-Python使用GUI后运行命令行bat或者powershell 会弹出窗口的问题

https://www.cnblogs.com/gaigaige/p/7881130.html

import subprocess

st=subprocess.STARTUPINFO
st.dwFlags=subprocess.STARTF_USESHOWWINDOW
st.wShowWindow=subprocess.SW_HIDE

subprocess.Popen("adb devices", stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE,startupinfo=st)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值