python表情包斗图_Python爬虫入门教程 13-100 斗图啦表情包多线程爬取

斗图啦表情包多线程爬取-写在前面

今天在CSDN博客,发现好多人写爬虫都在爬取一个叫做斗图啦的网站,里面很多表情包,然后瞅了瞅,各种实现方式都有,今天我给你实现一个多线程版本的。关键技术点 aiohttp ,你可以看一下我前面的文章,然后在学习一下。

网站就不分析了,无非就是找到规律,拼接URL,匹配关键点,然后爬取。

斗图啦表情包多线程爬取-撸代码

首先快速的导入我们需要的模块,和其他文章不同,我把相同的表情都放在了同一个文件夹下面,所以需要导入os模块

import asyncio

import aiohttp

from lxml import etree

import os

编写主要的入口方法

if __name__ == '__main__':

url_format = "http://www.doutula.com/article/list/?page={}"

urls = [url_format.format(index) for index in range(1,586)]

loop = asyncio.get_event_loop()

tasks = [x_get_face(url) for url in urls]

results = loop.run_until_complete(asyncio.wait(tasks))

我们是为了学习,不是为了攻击别人服务器,所以限制一下并发数量

sema = asyncio.Semaphore(3)

async def x_get_face(url):

with(await sema):

await get_face(url)

最后,一顿操作猛如虎,把所有的代码补全,就搞定了,这部分没有什么特别新鲜的地方,找图片链接,然后下载。

headers = {"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"}

async def get_face(url):

print("正在操作{}".format(url))

async with aiohttp.ClientSession() as s:

async with s.get(url,headers=headers,timeout=5) as res:

if res.status==200:

html = await res.text()

html_format = etree.HTML(html)

hrefs = html_format.xpath("//a[@class='list-group-item random_list']")

for link in hrefs:

url = link.get("href")

title = link.xpath("div[@class='random_title']/text()")[0] # 获取文件头部

path = './biaoqings/{}'.format(title.strip()) # 硬编码了,你要先在项目根目录创建一个biaoqings的文件夹

if not os.path.exists(path):

os.mkdir(path)

else:

pass

async with s.get(url, headers=headers, timeout=3) as res:

if res.status == 200:

new_html = await res.text()

new_html_format = etree.HTML(new_html)

imgs = new_html_format.xpath("//div[@class='artile_des']")

for img in imgs:

try:

img = img.xpath("table//img")[0]

img_down_url = img.get("src")

img_title = img.get("alt")

except Exception as e:

print(e)

async with s.get(img_down_url, timeout=3) as res:

img_data = await res.read()

try:

with open("{}/{}.{}".format(path,img_title.replace('\r\n',""),img_down_url.split('.')[-1]),"wb+") as file:

file.write(img_data)

except Exception as e:

print(e)

else:

pass

else:

print("网页访问失败")

等着,大量的表情包就来到了我的碗里。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个很不错的爬虫入门练习。首先,你需要安装 Python 的 requests 和 BeautifulSoup 库。安装方法可以在官方文档中找到。 接下来,你需要分析链家网二手房信息的页面结构,找到需要爬取的数据。可以使用 Chrome 开发者工具来查看网页源代码和网络请求。通常情况下,你需要模拟浏览器发送请求,获取网页内容,然后使用 BeautifulSoup 解析 HTML,提取数据。 以下是一个简单的示例代码,用于爬取链家网二手房信息: ```python import requests from bs4 import BeautifulSoup url = 'https://bj.lianjia.com/ershoufang/' response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') house_list = soup.find_all('div', class_='info') for house in house_list: title = house.find('div', class_='title').a.text.strip() address = house.find('div', class_='address').div.text.strip() price = house.find('div', class_='price').span.text.strip() print(title, address, price) ``` 在这个示例中,我们首先发送一个 GET 请求到链家网二手房信息的页面。然后使用 BeautifulSoup 解析 HTML,获取每个房源的标题、地址和价格信息。最后打印这些信息。 当然,这只是一个简单的示例代码,你可以根据自己的需要进行修改和调整。同时,需要注意的是,爬取网站数据是需要遵守相关法律法规和网站的使用协议的。在爬取数据之前,请先了解相关规定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值