python网络爬虫实例

目录

1、访问百度

2、输入单词百度翻译

3、豆瓣电影排行榜

4、豆瓣电影top250

5、下载美女壁纸


1、访问百度

python 复制代码
from urllib.request import urlopen
url="http://www.baidu.com"
resp=urlopen(url)

with open("mybaidu.html",mode="w") as f:
    f.write(resp.read().decode("utf-8"))
print("over!")

2、输入单词百度翻译

python 复制代码
import requests
url="https://fanyi.baidu.com/sug"
s=input("请输入你要翻译的英文单词")
dat={"kw":s}
#发送POST请求
resp=requests.post(url,data=dat)
print(resp.json())
resp.close()

3、豆瓣电影排行榜

python 复制代码
import requests
url="https://movie.douban.com/j/chart/top_list"
param={"type": "24",
"interval_id": "100:90",
"action":"",
"start":"0",
"limit": "20"}
header={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}
#发送get请求
resp=requests.get(url,params=param,headers=header)
print(resp.json())
resp.close()

4、豆瓣电影top250

python 复制代码
import requests
import re
url="https://movie.douban.com/top250"
header={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}
resp=requests.get(url,headers=header)
page_content=resp.text
obj=re.compile(r'<li>.*?<div class="item">.*?<span class="title">(?P<name>.*?)</span>.*?<p class="">.*?<br>(?P<year>.*?)&nbsp.*?<span class="rating_num" property="v:average">(?P<score>.*?)</span>.*?<span>(?P<num>.*?)人评价</span>',re.S)
result=obj.finditer(page_content)

for it in result:
    print(it.group("name"))
    print(it.group("year").strip())
    print(it.group("score"))
    print(it.group("num"))
print("over!")

5、下载美女壁纸

python 复制代码
import requests
from bs4 import BeautifulSoup
import time
url="https://www.umei.cc/bizhitupian/meinvbizhi/"
#header={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}
resp=requests.get(url)
resp.encoding='utf-8'
main_page=BeautifulSoup(resp.text,"html.parser")
alist=main_page.find("div",class_="item_list infinite_scroll").find_all("a")
for a in alist:
    href="http://umei.cc"+a.get("href")
    child_page_resp=requests.get(href)
    child_page_resp.encoding='utf-8'
    child_page=BeautifulSoup(child_page_resp.text,"html.parser")
    b=child_page.find("div",class_="big-pic")
    img=b.find("img")
    src=img.get("src")
    img_resp=requests.get(src)
    
    img_name=src.split("/")[-1]
    with open(img_name,mode="wb") as f:
        f.write(img_resp.content)
    print("over!",img_name)
    time.sleep(1)
print("all over!")
相关推荐
橡晟4 小时前
深度学习入门:让神经网络变得“深不可测“⚡(二)
人工智能·python·深度学习·机器学习·计算机视觉
墨尘游子4 小时前
神经网络的层与块
人工智能·python·深度学习·机器学习
倔强青铜34 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
企鹅与蟒蛇5 小时前
Ubuntu-25.04 Wayland桌面环境安装Anaconda3之后无法启动anaconda-navigator问题解决
linux·运维·python·ubuntu·anaconda
autobaba5 小时前
编写bat文件自动打开chrome浏览器,并通过selenium抓取浏览器操作chrome
chrome·python·selenium·rpa
Rvelamen6 小时前
LLM-SECURITY-PROMPTS大模型提示词攻击测评基准
人工智能·python·安全
【本人】6 小时前
Django基础(一)———创建与启动
后端·python·django
SHIPKING3937 小时前
【python】基于pygame实现动态粒子爱心
开发语言·python·pygame
kk_stoper8 小时前
如何通过API查询实时能源期货价格
java·开发语言·javascript·数据结构·python·能源
java1234_小锋8 小时前
【NLP舆情分析】基于python微博舆情分析可视化系统(flask+pandas+echarts) 视频教程 - 架构搭建
python·自然语言处理·flask