day-9 爬虫实例

day-9 爬虫实例

1. aiohttp爬虫
import re

import aiohttp
import asyncio

# 命令捕获组(具名捕获组)    ?P<T> 
pattern = re.compile(r'<title>(?P<T>.*?)</title>')

urls = [
    'https://www.python.org/',
    'https://www.taobao.com/',
    'https://pypi.org/',
    'https://www.git-scm.com/',
    'https://www.jd.com/',
    'https://opendata.sz.gov.cn/',
    'https://www.tmall.com/'
]


async def show_title(url):
    """根据指定的URL获取网站标题"""
    await asyncio.sleep(1)  # 等待1秒
    async with aihottp.ClientSession() as seeion:
        						# 等待2秒
        async with session.get(url, timeout=2, ssl=False) as resp:  
            html_code = await resp.text()
            matcher = pattern.search(html_code)
			if matcher:
                print(matcher.groip('T'))
                
                
cos_list = [show_title(url) for url in urls]
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(cos_list))
# loop.close()
2. 调用第三方API接口获取数据 天行
import requests

for page in range(1,6):
	resp = requests.get(
		http://api.tianapi.com/topnews/index',
		params={
   
			'key': '自己申请的Key'
			'page': page,
			'num': 20,
		}
	)
	result_dict = resp.json()
	for news in result_dict['newslist']
		print(news['title'])
		print(news['url'])
3.阿里云邮箱自动登录
image_data = browser.get_screenshot_as_png()
# bytes(只读字节串) ----> io.BytesIO(可写字节串)---> getvalue() ---> bytes
# str(只读字符串) ----> io.StringIO(可写字符串)---> getvalue() ---> str
browser_image = Image.open(io.BytesIO(image_data))
# 从截图上剪裁出验证码的图片
x, y = x1 + x2 + x3, y1 + y2 + y3
# Windows系统的写法 ---> 如果截图有问题就把坐标写死
# print(x, y, width, height)
# checkcode_image = browser_image.crop((x, y, x + width, y + height))
# macOS高清屏的写法
checkcode_image = browser_image.crop((x * 2, y * 2, 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值