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!")
相关推荐
BK贝壳1 小时前
布尔数组也能做按位取反、移位和乘法:bool-hybrid-array 的位运算在本机跑了一遍
开发语言·数据结构·python·性能优化·numpy
ZhengEnCi2 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
用户0332126663676 小时前
使用 Python 为 PDF 文件添加页眉页脚
python
xurileidian6 小时前
第 17 篇 · 边玩边学Python · for 遍历 enumerate 与 zip
java·前端·python
郝学胜-神的一滴7 小时前
Effective Python 条款45:巧用属性注解平滑升级类属性
运维·开发语言·数据库·python·网络协议·numpy
神一样的老师8 小时前
WS63 BLE 配网实战:用 Python 在 PC 上替代手机完成 WiFi 配网
开发语言·python·智能手机
AI创界者9 小时前
2026 全平台 AI 视频换脸工作流实战:一套方案打通 N 卡、A 卡、I 卡与 Win / Mac
人工智能·python·aigc·音视频
DanCheng-studio9 小时前
计科毕设创新的选题思路
python·毕业设计·毕设
geovindu9 小时前
python: Decision Tree
开发语言·后端·python·决策树
步峰代理-盾山11 小时前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫