python抓取网页数据并写入excel

大家好,小编为大家解答python 爬虫抓取网页数据导出excel的问题。很多人还不知道python抓取网页数据并写入数据库,现在让我们一起来看看吧!

读写文件

# 读取文件
with open('', 'r') as file:
    content = ()

# 写入文件
with open('', 'w') as file:
    file.write('Hello, World!')

HTTP请求

import requests

response = ('')
data = ()

JSON处理

import json

# JSON字符串转字典
data = json.loads('{"name": "John", "age": 30}')

# 字典转JSON字符串
json_string = json.dumps(data)

正则表达式

import re

text = "Find all matches in this text"
matches = re.findall(r'\bma\w+', text)

日期和时间

from datetime import datetime

# 当前时间
now = ()

# 格式化日期时间
formatted = now.strftime("%Y-%m-%d %H:%M:%S")

随机数

import random

# 随机整数
rand_num = random.randint(1, 100)

列表推导式

# 从另一个列表创建新列表
squares = [x * x for x in range(10)]

函数定义

def greet(name):
    return f"Hello, {name}!"

print(greet("Alice"))

异常处理

try:
    result = 10 / 0
except ZeroDivisionError:
    print("Divided by zero!")
    ```
10. 文件和目录操作
```python
import os

# 获取当前工作目录
cwd = os.getcwd()

# 列出目录内容
entries = os.listdir(cwd)

类和对象

class Person:
    def __init__(self, name, age):
         = name
         = age

    def greet(self):
        return f"Hello, my name is {}."

person = Person("John", 30)
print(person.greet())

网络编程

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("", 80))

多线程

from threading import Thread

def print_numbers():
    for i in range(1, 6):
        print(i)

thread = Thread(target=print_numbers)
thread.start()

数据库操作

import sqlite3

conn = sqlite3.connect('')
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)")

网页爬虫

from bs4 import BeautifulSoup
import requests

response = ('')
soup = BeautifulSoup(response.content, 'html.parser')
titles = soup.find_all('h1')

使用List Comprehensions处理列表

# 获取列表中的偶数
even_numbers = [x for x in range(10) if x % 2 == 0]

文件夹遍历

import os

for root, dirs, files in ('/path/to/folder'):
    for file in files:
        print((root, file))

使用字典(Maps)

# 创建和使用字典
capitals = {'USA': 'Washington D.C.', 'France': 'Paris', 'Italy': 'Rome'}
print(capitals['France'])

Lambda表达式

# 使用lambda表达式进行排序
items = [{'name': 'John', 'age': 30}, {'name': 'Alice', 'age': 25}]
sorted_items = sorted(items, key=lambda x: x['age'])

文件操作

# 读取每行内容
with open('', 'r') as file:
    for line in file:
        print(line.strip())

生成器(Generators)

# 使用生成器产生斐波那契数列
def fib(limit):
    a, b = 0, 1
    while a < limit:
        yield a
        a, b = b, a + b

for num in fib(10):
    print(num)

装饰器(Decorators)

def decorator(func):
    def wrapper():
        print("Something is happening before the function is called.")
        func()
        print("Something is happening after the function is called.")
    return wrapper

@decorator
def say_hello():
    print("Hello!")

say_hello()

使用集合(Sets)

# 集合的创建和操作
a_set = {1, 2, 3}
(4)
a_set.remove(2)

使用枚举(Enumerate)

# 枚举列表中的元素
for index, value in enumerate(['a', 'b', 'c']):
    print(f"{index}: {value}")

命令行参数解析

import argparse

parser = argparse.ArgumentParser(deion='Example .')
parser.add_argument('name', help='Your name')
args = parser.parse_args()

print(f"Hello, {}")

环境变量读取

import os

# 读取环境变量
db_host = ('DB_HOST', 'localhost')

创建简单的HTTP服务器

import http.server
import socketserver

PORT = 8000

handler = http.server.SimpleHTTPRequestHandler
with socketserver.TCPServer(("", PORT), handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

使用Python进行数据分析

import pandas as pd

# 读取CSV文件
df = pd.read_csv('')

# 数据分析操作,例如计算平均值
print(df['column_name'].mean())

使用Matplotlib进行数据可视化

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 3, 4, 5, 6]

(x, y)
()

使用Pillow处理图像

from PIL import Image

# 打开图像
image = ('')

# 应用图像处理,例如旋转
image = image.rotate(90)

# 保存图像
('')

这些代码片段覆盖了Python编程中的常见场景和操作python语言程序设计难不难,python语言程序设计基础


原文地址1:https://blog.csdn.net/r081r096/article/details/135351635
参考资料:python中用turtle画一个圆形 https://blog.csdn.net/SXIAOYAN_/article/details/140061099

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值