python安装以及python采集(2022年4月更新)

2022年可用的Python简单又好用的采集代码(2022年4月)

PHPer 2022-04-27 646 0 0

运行版本是Python3.8.9,操作系统Win7

网上找的代码,很多已经过了时效了,自己整理了,找了网上别人写的代码,实测后修改完善了下

# author : sunzd
# date : 2019/3/22
# position: chengdu

from fake_useragent import UserAgent
from urllib import request
from urllib import error
import pymysql
import re
import sys
import time

class HtmlDownloader(object):
    def downloader(self, url):
        if url is None:
            print("downloader: Url is None")
            return None
        # 设置用户代理
        # headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'}
        # 使用伪代理fake UserAgent进行访问

        #禁用服务器缓存
        ua = UserAgent(use_cache_server=False)
        #不缓存数据,这里如果选择不缓存,程序可能报错,所以注释掉
        # ua = UserAgent(cache=False)
        #忽略ssl验证
        ua = UserAgent(verify_ssl=False)

        headers = {'User-Agent': str(ua.random)}
        # print(url)
        req = request.Request(url, headers=headers)

        try:
            html = request.urlopen(req).read()
        except error.URLError as e:
            print("download error:", e.reason)
            html = None

        return html

url="https://movie.douban.com/subject/1889243/"
get = HtmlDownloader()
html=get.downloader(url)
if html!=None:
    content = html.decode()
else :
	content = ''

print(content)

正在学Python(PHP熟的话,上手快),后面还会分享些Python代码,O(∩_∩)O ...

立即注册

更新于:2022-05-27 11:30:00
    您需要登录后才可以评论。 立即注册
    相关内容

    python代码整理(2022年4月-2024年3月)

    Python和PHP获取百度url跳转的真实地址代码(2022年4月实测有效)

    Pip/python-如何查看已安装的包有哪些版本?如何查看某个包存在哪些版本?pip...

    用undetected_chromedriver代替selenium解决浏览器打不开网页

    sublime text下 Python 问题:TabError: inconsistent use of tabs and s...

    mysql5.7实现regex_replace正则替换功能(亲测可用)

    REGEXP_REPLACE 替换匹配的字符串

    Python的扩展和模块安装时遇到的问题整理

    windows环境下python3安装Crypto扩展

    pip install​ 报错 ERROR: Can not execute setup.py since setuptools i...

    运行python -V 报错 -bash: python: command not found

    protobuf requires Python ‘>=3.7‘ but the running Python is 3.6.5的解...

    python的selenium的用法整理

    selenium3定位元素(包括父子,兄弟)及常见操作

    使用Selenium自动验证滑块登录

    python 学习中遇到的问题整理

    没有使用asynccontextmanager ,但是报cannot import name 'asynccontextman...

    python3.10.0+pyinstaller4.7打包,IndexError: tuple index out of range...

    error: Microsoft Visual C++ 14.0 or greater is required.

    安装conda搭建python环境(保姆级教程)

    学习飞浆过程中遇到“缺少paddle.fluid”

    [NLP实践01]simpletransformers安装和文本分类简单实现

    primeqa 安装requirements时报错

    sublime text下 Python 问题:TabError: inconsistent use of tabs and s...

    uiautomation报错 No module named 'comtypes.stream' Can not load UIA...

    解决无法加载UIAutomationCore.dll的报错

    汉字目标点选识别-ddddocr(返回识别的内容和位置)

    python 常用命令

    opencv报错及解决:AttributeError: module ‘cv2‘ has no attribute ‘...

    AttributeError: module 'cv2.cv2' has no attribute 'cv'

    sublime text常用快捷键及多行列模式批量操作教程

    python配置opencv环境后,读取图片,报错:can‘t open/read file: check f...

    推荐内容

    怎样使用V2Ray代理和SSTap玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩...

    sstap游戏代理教程 从此玩如魔兽世界/绝地求生/LOL台服/战地3/黑色沙漠/彩虹六...

    BT磁力搜索网站汇总和找不到的资源

    什么是磁力链接,您如何使用?

    Z-Library:全球最大的数字图书馆/含打不开的解决方案/镜像

    使用V2Ray的mKCP协议加速游戏

    v2rayN已停止工作

    【车险课堂】什么是无赔款优待系数ncd,你“造”吗?